Skip to content

Commit

Permalink
HaikuDepot : additional debugging for json-rpc invocations
Browse files Browse the repository at this point in the history
  • Loading branch information
andponlin authored and pulkomandy committed Mar 25, 2018
1 parent 8234666 commit b45e8b1
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 22 deletions.
2 changes: 2 additions & 0 deletions headers/private/shared/Json.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
65 changes: 47 additions & 18 deletions src/apps/haikudepot/server/WebAppInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -625,14 +639,29 @@ WebAppInterface::_SendJsonRequest(const char* domain, BString jsonString,
return B_ERROR;
}

jsonString.SetTo(static_cast<const char*>(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<const char *>(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<const char *>(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);
}

6 changes: 5 additions & 1 deletion src/apps/haikudepot/server/WebAppInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,11 @@ class WebAppInterface {
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;
Expand Down
13 changes: 10 additions & 3 deletions src/kits/shared/Json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<BMemoryIO> inputDeleter(input);
BJsonMessageWriter* writer = new BJsonMessageWriter(message);
ObjectDeleter<BJsonMessageWriter> writerDeleter(writer);
Expand All @@ -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
Expand Down Expand Up @@ -768,4 +775,4 @@ BJson::ParseNumber(JsonParseContext& jsonParseContext)
}
}

} // namespace BPrivate
} // namespace BPrivate

0 comments on commit b45e8b1

Please sign in to comment.