Skip to content

Commit

Permalink
Continuing to implement xml-rpc
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewshell committed Jan 31, 2020
1 parent d2641ab commit 6dd2def
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 33 deletions.
2 changes: 2 additions & 0 deletions bin/import-data.js
Expand Up @@ -27,6 +27,8 @@ async function doImport() {
pleaseNotify: Object.keys(data.subscriptions[id]).map(sid => {
const subscription = data.subscriptions[id][sid];
subscription.url = sid;
subscription.notifyProcedure = false;
subscription.protocol = 'http-post';
return subscription;
})
}
Expand Down
6 changes: 2 additions & 4 deletions client.js
Expand Up @@ -41,15 +41,13 @@ app = express();

app.use(morgan('[:mydate] :method :url :status :res[content-length] - :remote-addr - :response-time ms'));

app.use(urlencodedParser);

app.use(express.static('public', {
dotfiles: 'ignore',
maxAge: '1d'
}));

app.post('/RPC2', textParser, function (req, res) {
console.log('post');
console.log('rpc');
console.dir(req.body);
res.send('');
})
Expand All @@ -61,7 +59,7 @@ app.get('/*', function (req, res) {
res.send(challenge);
});

app.post('/*', function (req, res) {
app.post('/*', urlencodedParser, function (req, res) {
console.log('post');
console.dir(req.body);
res.send('');
Expand Down
8 changes: 6 additions & 2 deletions controllers/rpc2.js
Expand Up @@ -33,6 +33,10 @@
parseRpcRequest(req)
.then(request => {
switch (request.methodName) {
case 'rssCloud.hello':
console.log(request.params[0]);
processResponse(req, res, rpcReturnSuccess(true));
break;
case 'rssCloud.pleaseNotify':
const params = parseNotifyParams.rpc(req, request.params);
pleaseNotify(
Expand All @@ -42,12 +46,12 @@
params.urlList,
params.diffDomain
)
.then(result => processResponse(req, res, rpcReturnSuccess(result.success, result.msg)))
.then(result => processResponse(req, res, rpcReturnSuccess(result.success)))
.catch(err => handleError(req, res, err));
break;
case 'rssCloud.ping':
ping(request.params[0])
.then(result => processResponse(req, res, rpcReturnSuccess(result.success, result.msg)))
.then(result => processResponse(req, res, rpcReturnSuccess(result.success)))
.catch(err => handleError(req, res, err));
break;
default:
Expand Down
6 changes: 4 additions & 2 deletions services/init-subscription.js
Expand Up @@ -4,15 +4,17 @@
const config = require('../config'),
moment = require('moment');

function initSubscription(subscriptions, apiurl) {
function initSubscription(subscriptions, notifyProcedure, apiurl, protocol) {
const defaultSubscription = {
ctUpdates: 0,
whenLastUpdate: moment.utc('0', 'x').format(),
ctErrors: 0,
ctConsecutiveErrors: 0,
whenLastError: moment.utc('0', 'x').format(),
whenExpires: moment().utc().add(config.ctSecsResourceExpire, 'seconds').format(),
url: apiurl
url: apiurl,
notifyProcedure,
protocol
};

const index = subscriptions.pleaseNotify.findIndex(subscription => {
Expand Down
44 changes: 41 additions & 3 deletions services/notify-one.js
@@ -1,13 +1,14 @@
(function () {
"use strict";

const request = require('request-promise-native');
const builder = require('xmlbuilder'),
request = require('request-promise-native');

async function notifyOne(resourceUrl, apiurl) {
async function notifyOneRest(apiurl, resourceUrl) {
const res = await request({
method: 'POST',
uri: apiurl,
data: {
form: {
'url': resourceUrl
},
resolveWithFullResponse: true
Expand All @@ -20,5 +21,42 @@
return true;
}

async function notifyOneRpc(notifyProcedure, apiurl, resourceUrl) {
const xmldoc = builder.create({
methodCall: {
methodName: notifyProcedure,
params: {
param: [
{ value: resourceUrl }
]
}
}
});

const res = await request({
method: 'POST',
uri: apiurl,
body: xmldoc,
resolveWithFullResponse: true,
headers: {
'content-type': 'text/xml'
}
});

if (res.statusCode < 200 || res.statusCode > 299) {
throw new Error('Notification Failed');
}

return true;
}

function notifyOne(notifyProcedure, apiurl, protocol, resourceUrl) {
if ('xml-rpc' === protocol) {
return notifyOneRpc(notifyProcedure, apiurl, resourceUrl);
}

return notifyOneRest(apiurl, resourceUrl);
}

module.exports = notifyOne;
}());
8 changes: 4 additions & 4 deletions services/parse-notify-params.js
Expand Up @@ -4,7 +4,7 @@
const appMessages = require('./app-messages'),
sprintf = require('sprintf-js').sprintf;

function validateProtocol(protocol) {
function validProtocol(protocol) {
switch (protocol) {
case 'http-post':
case 'https-post':
Expand Down Expand Up @@ -126,9 +126,9 @@
params.notifyProcedure = false;
}

parts.scheme = 'https-post' === parts.protocol ? 'https' : 'http';
parts.port = req.body.port;
parts.path = req.body.path;
parts.scheme = 'http';
parts.port = rpcParams[1];
parts.path = rpcParams[2];

params.apiurl = glueUrlParts(
parts.scheme,
Expand Down
6 changes: 5 additions & 1 deletion services/parse-rpc-request.js
Expand Up @@ -39,7 +39,11 @@
}, {});
break;
case 'array':
returnedValue = ((value[tag].data || {}).value || []).map(parseRpcParam);
let values = (value[tag].data || {}).value || [];
if (!Array.isArray(values)) {
values = [values];
}
returnedValue = values.map(parseRpcParam);
break;
}
}
Expand Down
8 changes: 4 additions & 4 deletions services/please-notify.js
Expand Up @@ -50,18 +50,18 @@
);
}

async function notifyApiUrl(resourceUrl, apiurl, diffDomain) {
async function notifyApiUrl(notifyProcedure, apiurl, protocol, resourceUrl, diffDomain) {
const subscriptions = await fetchSubscriptions(resourceUrl),
startticks = moment().format('x'),
parts = url.parse(apiurl);

initSubscription(subscriptions, apiurl);
initSubscription(subscriptions, notifyProcedure, apiurl, protocol);

try {
if (diffDomain) {
await notifyOneChallenge(resourceUrl, apiurl);
} else {
await notifyOne(resourceUrl, apiurl);
await notifyOne(notifyProcedure, apiurl, protocol, resourceUrl);
}

const index = subscriptions.pleaseNotify.findIndex(subscription => {
Expand Down Expand Up @@ -96,7 +96,7 @@
for (resourceUrl of urlList) {
try {
await checkresourceUrlStatusCode(resourceUrl);
await notifyApiUrl(resourceUrl, apiurl, diffDomain);
await notifyApiUrl(notifyProcedure, apiurl, protocol, resourceUrl, diffDomain);
} catch (err) {
lastErr = err;
}
Expand Down
9 changes: 2 additions & 7 deletions services/rpc-return-success.js
Expand Up @@ -3,7 +3,7 @@

const builder = require('xmlbuilder');

function rpcReturnSuccess(success, message) {
function rpcReturnSuccess(success) {
return builder.create({
methodResponse: {
params: {
Expand All @@ -12,12 +12,7 @@
value: {
boolean: success ? 1 : 0
}
},
{
value: {
string: message
}
},
}
]
}
}
Expand Down
6 changes: 0 additions & 6 deletions test.js

This file was deleted.

0 comments on commit 6dd2def

Please sign in to comment.