HaikuDepot : Fixes for User-Ratings Related API Calls
Corrections to some areas where the 'position' of a data stream was not being set correctly before reading. Change-Id: I0030a113008028d5480dc36d034cf06915d928de Reviewed-on: https://review.haiku-os.org/588 Reviewed-by: Stephan Aßmus <[email protected]>
This commit is contained in:
@@ -17,6 +17,7 @@
|
|||||||
#include <HttpRequest.h>
|
#include <HttpRequest.h>
|
||||||
#include <Json.h>
|
#include <Json.h>
|
||||||
#include <JsonTextWriter.h>
|
#include <JsonTextWriter.h>
|
||||||
|
#include <JsonMessageWriter.h>
|
||||||
#include <Message.h>
|
#include <Message.h>
|
||||||
#include <Roster.h>
|
#include <Roster.h>
|
||||||
#include <Url.h>
|
#include <Url.h>
|
||||||
@@ -418,7 +419,8 @@ WebAppInterface::RetrieveUserRating(const BString& packageName,
|
|||||||
requestEnvelopeWriter.WriteObjectEnd();
|
requestEnvelopeWriter.WriteObjectEnd();
|
||||||
|
|
||||||
return _SendJsonRequest("userrating", requestEnvelopeData,
|
return _SendJsonRequest("userrating", requestEnvelopeData,
|
||||||
requestEnvelopeData->Position(), NEEDS_AUTHORIZATION, message);
|
_LengthAndSeekToZero(requestEnvelopeData), NEEDS_AUTHORIZATION,
|
||||||
|
message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -498,7 +500,8 @@ WebAppInterface::CreateUserRating(const BString& packageName,
|
|||||||
requestEnvelopeWriter.WriteObjectEnd();
|
requestEnvelopeWriter.WriteObjectEnd();
|
||||||
|
|
||||||
return _SendJsonRequest("userrating", requestEnvelopeData,
|
return _SendJsonRequest("userrating", requestEnvelopeData,
|
||||||
requestEnvelopeData->Position(), NEEDS_AUTHORIZATION, message);
|
_LengthAndSeekToZero(requestEnvelopeData), NEEDS_AUTHORIZATION,
|
||||||
|
message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -556,7 +559,8 @@ WebAppInterface::UpdateUserRating(const BString& ratingID,
|
|||||||
requestEnvelopeWriter.WriteObjectEnd();
|
requestEnvelopeWriter.WriteObjectEnd();
|
||||||
|
|
||||||
return _SendJsonRequest("userrating", requestEnvelopeData,
|
return _SendJsonRequest("userrating", requestEnvelopeData,
|
||||||
requestEnvelopeData->Position(), NEEDS_AUTHORIZATION, message);
|
_LengthAndSeekToZero(requestEnvelopeData), NEEDS_AUTHORIZATION,
|
||||||
|
message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -706,9 +710,15 @@ WebAppInterface::_WriteStandardJsonRpcEnvelopeValues(BJsonWriter& writer,
|
|||||||
|
|
||||||
|
|
||||||
status_t
|
status_t
|
||||||
WebAppInterface::_SendJsonRequest(const char* domain, BDataIO* requestData,
|
WebAppInterface::_SendJsonRequest(const char* domain, BPositionIO* requestData,
|
||||||
size_t requestDataSize, uint32 flags, BMessage& reply) const
|
size_t requestDataSize, uint32 flags, BMessage& reply) const
|
||||||
{
|
{
|
||||||
|
if (requestDataSize == 0) {
|
||||||
|
if (Logger::IsInfoEnabled())
|
||||||
|
printf("jrpc; empty request payload\n");
|
||||||
|
return B_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
if (!ServerHelper::IsNetworkAvailable()) {
|
if (!ServerHelper::IsNetworkAvailable()) {
|
||||||
if (Logger::IsDebugEnabled()) {
|
if (Logger::IsDebugEnabled()) {
|
||||||
printf("jrpc; dropping request to ...[%s] as network is not "
|
printf("jrpc; dropping request to ...[%s] as network is not "
|
||||||
@@ -740,22 +750,11 @@ WebAppInterface::_SendJsonRequest(const char* domain, BDataIO* requestData,
|
|||||||
// delivered from memory.
|
// delivered from memory.
|
||||||
|
|
||||||
if (Logger::IsTraceEnabled()) {
|
if (Logger::IsTraceEnabled()) {
|
||||||
BMallocIO *loggedRequestData = new BMallocIO();
|
off_t requestDataPosition = requestData->Position();
|
||||||
loggedRequestData->SetSize(requestDataSize);
|
|
||||||
status_t dataCopyResult = DataIOUtils::Copy(loggedRequestData,
|
|
||||||
requestData, requestDataSize);
|
|
||||||
delete requestData;
|
|
||||||
requestData = loggedRequestData;
|
|
||||||
|
|
||||||
if (dataCopyResult != B_OK) {
|
|
||||||
delete requestData;
|
|
||||||
return dataCopyResult;
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("jrpc request; ");
|
printf("jrpc request; ");
|
||||||
_LogPayload(static_cast<const char *>(loggedRequestData->Buffer()),
|
_LogPayload(requestData, requestDataSize);
|
||||||
loggedRequestData->BufferLength());
|
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
requestData->Seek(requestDataPosition, SEEK_SET);
|
||||||
}
|
}
|
||||||
|
|
||||||
ProtocolListener listener(Logger::IsTraceEnabled());
|
ProtocolListener listener(Logger::IsTraceEnabled());
|
||||||
@@ -779,6 +778,7 @@ WebAppInterface::_SendJsonRequest(const char* domain, BDataIO* requestData,
|
|||||||
context.AddAuthentication(url, authentication);
|
context.AddAuthentication(url, authentication);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
request.AdoptInputData(requestData, requestDataSize);
|
request.AdoptInputData(requestData, requestDataSize);
|
||||||
|
|
||||||
BMallocIO replyData;
|
BMallocIO replyData;
|
||||||
@@ -813,14 +813,16 @@ WebAppInterface::_SendJsonRequest(const char* domain, BDataIO* requestData,
|
|||||||
|
|
||||||
if (Logger::IsTraceEnabled()) {
|
if (Logger::IsTraceEnabled()) {
|
||||||
printf("jrpc response; ");
|
printf("jrpc response; ");
|
||||||
_LogPayload(static_cast<const char *>(replyData.Buffer()),
|
replyData.Seek(0, SEEK_SET);
|
||||||
replyData.BufferLength());
|
_LogPayload(&replyData, replyData.BufferLength());
|
||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
status_t status = BJson::Parse(
|
replyData.Seek(0, SEEK_SET);
|
||||||
static_cast<const char *>(replyData.Buffer()), replyData.BufferLength(),
|
BJsonMessageWriter jsonMessageWriter(reply);
|
||||||
reply);
|
BJson::Parse(&replyData, &jsonMessageWriter);
|
||||||
|
status_t status = jsonMessageWriter.ErrorStatus();
|
||||||
|
|
||||||
if (Logger::IsTraceEnabled() && status == B_BAD_DATA) {
|
if (Logger::IsTraceEnabled() && status == B_BAD_DATA) {
|
||||||
BString resultString(static_cast<const char *>(replyData.Buffer()),
|
BString resultString(static_cast<const char *>(replyData.Buffer()),
|
||||||
replyData.BufferLength());
|
replyData.BufferLength());
|
||||||
@@ -831,12 +833,12 @@ WebAppInterface::_SendJsonRequest(const char* domain, BDataIO* requestData,
|
|||||||
|
|
||||||
|
|
||||||
status_t
|
status_t
|
||||||
WebAppInterface::_SendJsonRequest(const char* domain, BString jsonString,
|
WebAppInterface::_SendJsonRequest(const char* domain, const BString& jsonString,
|
||||||
uint32 flags, BMessage& reply) const
|
uint32 flags, BMessage& reply) const
|
||||||
{
|
{
|
||||||
// gets 'adopted' by the subsequent http request.
|
// gets 'adopted' by the subsequent http request.
|
||||||
BMemoryIO* data = new BMemoryIO(
|
BMemoryIO* data = new BMemoryIO(jsonString.String(),
|
||||||
jsonString.String(), jsonString.Length() - 1);
|
jsonString.Length() - 1);
|
||||||
|
|
||||||
return _SendJsonRequest(domain, data, jsonString.Length() - 1, flags,
|
return _SendJsonRequest(domain, data, jsonString.Length() - 1, flags,
|
||||||
reply);
|
reply);
|
||||||
@@ -844,21 +846,41 @@ WebAppInterface::_SendJsonRequest(const char* domain, BString jsonString,
|
|||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
WebAppInterface::_LogPayload(const char* data, ssize_t size)
|
WebAppInterface::_LogPayload(BPositionIO* requestData, size_t size)
|
||||||
{
|
{
|
||||||
|
char buffer[LOG_PAYLOAD_LIMIT];
|
||||||
|
|
||||||
if (size > LOG_PAYLOAD_LIMIT)
|
if (size > LOG_PAYLOAD_LIMIT)
|
||||||
size = LOG_PAYLOAD_LIMIT;
|
size = LOG_PAYLOAD_LIMIT;
|
||||||
|
|
||||||
for (int32 i = 0; i < size; i++) {
|
if (B_OK != requestData->ReadExactly(buffer, size)) {
|
||||||
bool esc = data[i] > 126 ||
|
printf("jrpc; error logging payload\n");
|
||||||
(data[i] < 0x20 && data[i] != 0x0a);
|
} else {
|
||||||
|
for (uint32 i = 0; i < size; i++) {
|
||||||
|
bool esc = buffer[i] > 126 ||
|
||||||
|
(buffer[i] < 0x20 && buffer[i] != 0x0a);
|
||||||
|
|
||||||
if (esc)
|
if (esc)
|
||||||
printf("\\u%02x", data[i]);
|
printf("\\u%02x", buffer[i]);
|
||||||
else
|
else
|
||||||
putchar(data[i]);
|
putchar(buffer[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (size == LOG_PAYLOAD_LIMIT)
|
||||||
|
printf("...(continues)");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if (size == LOG_PAYLOAD_LIMIT)
|
|
||||||
printf("...(continues)");
|
|
||||||
|
/*! This will get the position of the data to get the length an then sets the
|
||||||
|
offset to zero so that it can be re-read for reading the payload in to log
|
||||||
|
or send.
|
||||||
|
*/
|
||||||
|
|
||||||
|
off_t
|
||||||
|
WebAppInterface::_LengthAndSeekToZero(BPositionIO* data)
|
||||||
|
{
|
||||||
|
off_t dataSize = data->Position();
|
||||||
|
data->Seek(0, SEEK_SET);
|
||||||
|
return dataSize;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -113,13 +113,15 @@ private:
|
|||||||
BJsonWriter& writer,
|
BJsonWriter& writer,
|
||||||
const char* methodName);
|
const char* methodName);
|
||||||
status_t _SendJsonRequest(const char* domain,
|
status_t _SendJsonRequest(const char* domain,
|
||||||
BString jsonString, uint32 flags,
|
const BString& jsonString, uint32 flags,
|
||||||
BMessage& reply) const;
|
BMessage& reply) const;
|
||||||
status_t _SendJsonRequest(const char* domain,
|
status_t _SendJsonRequest(const char* domain,
|
||||||
BDataIO* requestData,
|
BPositionIO* requestData,
|
||||||
size_t requestDataSize, uint32 flags,
|
size_t requestDataSize, uint32 flags,
|
||||||
BMessage& reply) const;
|
BMessage& reply) const;
|
||||||
static void _LogPayload(const char* data, ssize_t size);
|
static void _LogPayload(BPositionIO* requestData,
|
||||||
|
size_t size);
|
||||||
|
static off_t _LengthAndSeekToZero(BPositionIO* data);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
BString fUsername;
|
BString fUsername;
|
||||||
|
|||||||
Reference in New Issue
Block a user