calling 'append' on JSONP-RPC

Get help, report and discuss bugs.
BreeeZe
Posts: 12
Joined: 06 Mar 2015, 22:19

calling 'append' on JSONP-RPC

Post by BreeeZe » 06 Mar 2015, 22:35

For the past few hours I've been playing around with jsonp-rpc api because I want to create a simple extension for safari.
But I can't figure out how the 'append' call works :(

According to the documentation (and the source), the signature of this method is :
// v13 (new param order and new result type):
// int append(string NZBFilename, string NZBContent, string Category, int Priority, bool AddToTop, bool AddPaused, string DupeKey, int DupeScore, string DupeMode)
But if I, for instance, call this url :

Code: Select all

http://localhost:6789/jsonprpc/append?callback=callback&=myfilename&=http%3A%2F%2Fnzbindex.nl%2F&=Series&=0&=false&=true&=none&=0&=force
It responds with:

Code: Select all

callback({
"version" : "1.1",
"error" : {
"name" : "JSONRPCError",
"code" : 2,
"message" : "Invalid parameter (AddTop)"
}
})
Which would mean it can't parse the 'AddTop' parameter to a boolean..
I've been going through the source code (file:daemon\remote\XmlRPC.cpp) and the order and formatting of my parameters should be correct :(

I tried calling the method by adding the parameters 1 by 1, and if I get to the 'AddTop' parameter and call the url like this:

Code: Select all

http://localhost:6789/jsonprpc/append?callback=callback&=myfilename&=http%3A%2F%2Fnzbindex.nl%2F&=Series&=0&=false
It complains about the next mandatory parameter in the method called 'DupeKey',which would mean it parsed the parameter 'AddTop' correctly..

but if I add the next parameter according to the method signature (which is 'AddPauzed') and then call this url:

Code: Select all

http://localhost:6789/jsonprpc/append?callback=callback&=myfilename&=http%3A%2F%2Fnzbindex.nl%2F&=Series&=0&=false&=false
It complains about not understanding the 'AddTop' parameter again :shock:

...

Please help :)

hugbug
Developer & Admin
Posts: 7536
Joined: 09 Sep 2008, 11:58
Location: Germany

Re: calling 'append' on JSONP-RPC

Post by hugbug » 06 Mar 2015, 22:59

The request must be send via POST. This is how jsonrpc works.

BreeeZe
Posts: 12
Joined: 06 Mar 2015, 22:19

Re: calling 'append' on JSONP-RPC

Post by BreeeZe » 06 Mar 2015, 23:03

hugbug wrote:The request must be send via POST. This is how jsonrpc works.
But I'm using the jsonP-rpc api, which supports GET.

At least it does on other calls.. like:

Code: Select all

http://localhost:6789/jsonprpc/writelog?callback=callback&=INFO&=test!

hugbug
Developer & Admin
Posts: 7536
Joined: 09 Sep 2008, 11:58
Location: Germany

Re: calling 'append' on JSONP-RPC

Post by hugbug » 07 Mar 2015, 00:22

Let me take a look tomorrow what's happening here.

BreeeZe
Posts: 12
Joined: 06 Mar 2015, 22:19

Re: calling 'append' on JSONP-RPC

Post by BreeeZe » 07 Mar 2015, 06:40

hugbug wrote:Let me take a look tomorrow what's happening here.
Thanks! Much appreciated :)

I looked at the source myself but I couldn't figure out what is going wrong just by looking at it..
It parses the parameters from the url by finding the '=' and '&' and sets the next position to search the string, so when testing with partial urls it seems strange to me that it first succeeds in parsing the 'AddTop' parameter, but once you add the next parameter to the url it fails..

BreeeZe
Posts: 12
Joined: 06 Mar 2015, 22:19

Re: calling 'append' on JSONP-RPC

Post by BreeeZe » 07 Mar 2015, 21:24

I downloaded the windows source and installed visual studio and I have a debug setup running so I can step through the code.
When it parses the first 'false' parameter in the method "NextParamAsStr" (called by "NextParamAsBool") in XmlRpc.cpp it somehow detects the length of the string as "-2" which is ofcourse wrong, I didn't yet figure out why, but I'll debug some more later or tomorrow.

BreeeZe
Posts: 12
Joined: 06 Mar 2015, 22:19

Re: calling 'append' on JSONP-RPC

Post by BreeeZe » 07 Mar 2015, 22:53

Found it :)

In the file XmlRPC.cpp in the method NextParamAsInt :

Code: Select all

bool XmlCommand::NextParamAsInt(int* iValue)
{
	if (m_eHttpMethod == XmlRpcProcessor::hmGet)
	{
		char* szParam = strchr(m_szRequestPtr, '=');
		if (!szParam)
		{
			return false;
		}
		*iValue = atoi(szParam + 1);
		m_szRequestPtr = szParam + 1; <==== Should be +2 !
		return true;
	}
It parses a single digit int and after that it sets the 'm_szRequestPtr' to the position of the int, and not on the position of the '&' like it does in the string version.
So the next time it enters the method 'NextParamAsStr' it searches for the start of the value by finding the '=' and searches for the end of the string by finding the '&' but because the pointer is before the '&' of the last parameter it finds the '&' before the '=' making the length : -2

Code: Select all

bool XmlCommand::NextParamAsStr(char** szValue)
{
	if (m_eHttpMethod == XmlRpcProcessor::hmGet)
	{
		char* szParam = strchr(m_szRequestPtr, '=');
		if (!szParam)
		{
			return false;
		}
		szParam++; // skip '='
		int iLen = 0;
		char* szParamEnd = strchr(m_szRequestPtr, '&');
		if (szParamEnd)
		{
			iLen = (int)(szParamEnd - szParam);
			szParam[iLen] = '\0';
		}
		else
		{
			iLen = strlen(szParam) - 1;
		}
		m_szRequestPtr = szParam + iLen + 1;
		*szValue = szParam;
		return true;
	}
I can work around it for now by ommiting the '&' after an int parameter making the url:

Code: Select all

http://localhost:6789/jsonprpc/append?callback=callback&=myfilename&=http://nzbindex.nl/&=Series&=0=false&=true&=&=0=force
:)

hugbug
Developer & Admin
Posts: 7536
Joined: 09 Sep 2008, 11:58
Location: Germany

Re: calling 'append' on JSONP-RPC

Post by hugbug » 07 Mar 2015, 23:16

Thank you for the fix.
The parameter passing via URL was never thoroughly tested.
That's why it's better to send them via POST.

BreeeZe
Posts: 12
Joined: 06 Mar 2015, 22:19

Re: calling 'append' on JSONP-RPC

Post by BreeeZe » 08 Mar 2015, 08:25

hugbug wrote:Thank you for the fix.
The parameter passing via URL was never thoroughly tested.
That's why it's better to send them via POST.
Thank YOU for providing us all with NZBGet :)

I would like to use POST but safari is quite strict about cross-site XHR, I can set the extension policy to 'Allow' but it also expects the server to respond with the appropriate response header (https://developer.apple.com/library/saf ... s/XHR.html)
It expects a valid "Access-Control-Allow-Origin" response which I suspect isn't provided by NZBGet, so that's why I switched to jsonP :)
But since I have a debug setup running I'll see if I can find out why and where, but even if I fit it, the problem is I'm running NZBGet on a Synology NAS and either have to wait for the synocommunity to provide me with a patched version or learn how to compile and/or patch the Synology package myself :)

I'll also look into creating a more robust patch for the jsonp api so int values > 9 can be parsed.

BreeeZe
Posts: 12
Joined: 06 Mar 2015, 22:19

Re: calling 'append' on JSONP-RPC

Post by BreeeZe » 08 Mar 2015, 08:27

hugbug wrote:Thank you for the fix.
The parameter passing via URL was never thoroughly tested.
That's why it's better to send them via POST.
Thank YOU for providing us all with NZBGet :)

I would like to use POST but safari is quite strict about cross-site XHR, I can set the extension policy to 'Allow' but it also expects the server to respond with the appropriate response header (https://developer.apple.com/library/saf ... s/XHR.html)
It expects a valid "Access-Control-Allow-Origin" response which I suspect isn't provided by NZBGet, so that's why I switched to jsonP :)
But since I have a debug setup running I'll see if I can find out why and where, but even if I fix it, the problem is I'm running NZBGet on a Synology NAS and either have to wait for the synocommunity to provide me with a patched version or learn how to compile and/or patch the Synology package myself :)

I'll also look into creating a more robust patch for the jsonp api so int values > 9 can be parsed.

Post Reply

Who is online

Users browsing this forum: No registered users and 9 guests