-
Notifications
You must be signed in to change notification settings - Fork 0
/
ctaccessor.js
60 lines (49 loc) · 1.33 KB
/
ctaccessor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
var querystring = require('querystring');
var http=Object();
http["http"]= require('http');
http["https"]= require('https');
var ct_options={};
exports.jsendReadData = jsendReadData;
exports.setOptions = setOptions;
function setOptions(options) {
ct_options=options;
}
function jsendReadData(q, data, success) {
var dataString = querystring.stringify(data);
var headers = {};
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': dataString.length
};
var options = {
host: ct_options.ct_host,
port: ct_options.ct_port,
path: ct_options.ct_path+"?q="+q,
method: "post",
headers: headers
};
var req = http[ct_options.ct_protocol].request(options, function(res) {
res.setEncoding('utf-8');
var responseString = '';
res.on('data', function(data) {
responseString += data;
});
res.on('end', function() {
try {
var responseObject = JSON.parse(responseString);
success(responseObject.status=="success", responseObject.data);
}
catch(e) {
console.log(responseString);
console.log(e);
console.error("Error by parsing CT-Answer");
}
});
});
req.write(dataString);
req.end();
req.on('error', function(e) {
console.error("Connection to CT failed:"+e);
console.log(options);
});
}