Skip to content

Commit 6dd2def

Browse files
committed
Continuing to implement xml-rpc
1 parent d2641ab commit 6dd2def

File tree

10 files changed

+70
-33
lines changed

10 files changed

+70
-33
lines changed

bin/import-data.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ async function doImport() {
2727
pleaseNotify: Object.keys(data.subscriptions[id]).map(sid => {
2828
const subscription = data.subscriptions[id][sid];
2929
subscription.url = sid;
30+
subscription.notifyProcedure = false;
31+
subscription.protocol = 'http-post';
3032
return subscription;
3133
})
3234
}

client.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,13 @@ app = express();
4141

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

44-
app.use(urlencodedParser);
45-
4644
app.use(express.static('public', {
4745
dotfiles: 'ignore',
4846
maxAge: '1d'
4947
}));
5048

5149
app.post('/RPC2', textParser, function (req, res) {
52-
console.log('post');
50+
console.log('rpc');
5351
console.dir(req.body);
5452
res.send('');
5553
})
@@ -61,7 +59,7 @@ app.get('/*', function (req, res) {
6159
res.send(challenge);
6260
});
6361

64-
app.post('/*', function (req, res) {
62+
app.post('/*', urlencodedParser, function (req, res) {
6563
console.log('post');
6664
console.dir(req.body);
6765
res.send('');

controllers/rpc2.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@
3333
parseRpcRequest(req)
3434
.then(request => {
3535
switch (request.methodName) {
36+
case 'rssCloud.hello':
37+
console.log(request.params[0]);
38+
processResponse(req, res, rpcReturnSuccess(true));
39+
break;
3640
case 'rssCloud.pleaseNotify':
3741
const params = parseNotifyParams.rpc(req, request.params);
3842
pleaseNotify(
@@ -42,12 +46,12 @@
4246
params.urlList,
4347
params.diffDomain
4448
)
45-
.then(result => processResponse(req, res, rpcReturnSuccess(result.success, result.msg)))
49+
.then(result => processResponse(req, res, rpcReturnSuccess(result.success)))
4650
.catch(err => handleError(req, res, err));
4751
break;
4852
case 'rssCloud.ping':
4953
ping(request.params[0])
50-
.then(result => processResponse(req, res, rpcReturnSuccess(result.success, result.msg)))
54+
.then(result => processResponse(req, res, rpcReturnSuccess(result.success)))
5155
.catch(err => handleError(req, res, err));
5256
break;
5357
default:

services/init-subscription.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,17 @@
44
const config = require('../config'),
55
moment = require('moment');
66

7-
function initSubscription(subscriptions, apiurl) {
7+
function initSubscription(subscriptions, notifyProcedure, apiurl, protocol) {
88
const defaultSubscription = {
99
ctUpdates: 0,
1010
whenLastUpdate: moment.utc('0', 'x').format(),
1111
ctErrors: 0,
1212
ctConsecutiveErrors: 0,
1313
whenLastError: moment.utc('0', 'x').format(),
1414
whenExpires: moment().utc().add(config.ctSecsResourceExpire, 'seconds').format(),
15-
url: apiurl
15+
url: apiurl,
16+
notifyProcedure,
17+
protocol
1618
};
1719

1820
const index = subscriptions.pleaseNotify.findIndex(subscription => {

services/notify-one.js

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
(function () {
22
"use strict";
33

4-
const request = require('request-promise-native');
4+
const builder = require('xmlbuilder'),
5+
request = require('request-promise-native');
56

6-
async function notifyOne(resourceUrl, apiurl) {
7+
async function notifyOneRest(apiurl, resourceUrl) {
78
const res = await request({
89
method: 'POST',
910
uri: apiurl,
10-
data: {
11+
form: {
1112
'url': resourceUrl
1213
},
1314
resolveWithFullResponse: true
@@ -20,5 +21,42 @@
2021
return true;
2122
}
2223

24+
async function notifyOneRpc(notifyProcedure, apiurl, resourceUrl) {
25+
const xmldoc = builder.create({
26+
methodCall: {
27+
methodName: notifyProcedure,
28+
params: {
29+
param: [
30+
{ value: resourceUrl }
31+
]
32+
}
33+
}
34+
});
35+
36+
const res = await request({
37+
method: 'POST',
38+
uri: apiurl,
39+
body: xmldoc,
40+
resolveWithFullResponse: true,
41+
headers: {
42+
'content-type': 'text/xml'
43+
}
44+
});
45+
46+
if (res.statusCode < 200 || res.statusCode > 299) {
47+
throw new Error('Notification Failed');
48+
}
49+
50+
return true;
51+
}
52+
53+
function notifyOne(notifyProcedure, apiurl, protocol, resourceUrl) {
54+
if ('xml-rpc' === protocol) {
55+
return notifyOneRpc(notifyProcedure, apiurl, resourceUrl);
56+
}
57+
58+
return notifyOneRest(apiurl, resourceUrl);
59+
}
60+
2361
module.exports = notifyOne;
2462
}());

services/parse-notify-params.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
const appMessages = require('./app-messages'),
55
sprintf = require('sprintf-js').sprintf;
66

7-
function validateProtocol(protocol) {
7+
function validProtocol(protocol) {
88
switch (protocol) {
99
case 'http-post':
1010
case 'https-post':
@@ -126,9 +126,9 @@
126126
params.notifyProcedure = false;
127127
}
128128

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

133133
params.apiurl = glueUrlParts(
134134
parts.scheme,

services/parse-rpc-request.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@
3939
}, {});
4040
break;
4141
case 'array':
42-
returnedValue = ((value[tag].data || {}).value || []).map(parseRpcParam);
42+
let values = (value[tag].data || {}).value || [];
43+
if (!Array.isArray(values)) {
44+
values = [values];
45+
}
46+
returnedValue = values.map(parseRpcParam);
4347
break;
4448
}
4549
}

services/please-notify.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,18 @@
5050
);
5151
}
5252

53-
async function notifyApiUrl(resourceUrl, apiurl, diffDomain) {
53+
async function notifyApiUrl(notifyProcedure, apiurl, protocol, resourceUrl, diffDomain) {
5454
const subscriptions = await fetchSubscriptions(resourceUrl),
5555
startticks = moment().format('x'),
5656
parts = url.parse(apiurl);
5757

58-
initSubscription(subscriptions, apiurl);
58+
initSubscription(subscriptions, notifyProcedure, apiurl, protocol);
5959

6060
try {
6161
if (diffDomain) {
6262
await notifyOneChallenge(resourceUrl, apiurl);
6363
} else {
64-
await notifyOne(resourceUrl, apiurl);
64+
await notifyOne(notifyProcedure, apiurl, protocol, resourceUrl);
6565
}
6666

6767
const index = subscriptions.pleaseNotify.findIndex(subscription => {
@@ -96,7 +96,7 @@
9696
for (resourceUrl of urlList) {
9797
try {
9898
await checkresourceUrlStatusCode(resourceUrl);
99-
await notifyApiUrl(resourceUrl, apiurl, diffDomain);
99+
await notifyApiUrl(notifyProcedure, apiurl, protocol, resourceUrl, diffDomain);
100100
} catch (err) {
101101
lastErr = err;
102102
}

services/rpc-return-success.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
const builder = require('xmlbuilder');
55

6-
function rpcReturnSuccess(success, message) {
6+
function rpcReturnSuccess(success) {
77
return builder.create({
88
methodResponse: {
99
params: {
@@ -12,12 +12,7 @@
1212
value: {
1313
boolean: success ? 1 : 0
1414
}
15-
},
16-
{
17-
value: {
18-
string: message
19-
}
20-
},
15+
}
2116
]
2217
}
2318
}

test.js

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)