1 var http = require(
'http')
2 , https = require(
'https')
4 , Serializer = require(
'./serializer')
5 , Deserializer = require(
'./deserializer')
6 , Cookies = require(
'./cookies')
25 function Client(options, isSecure) {
28 if (
false === (
this instanceof Client)) {
29 return new Client(options, isSecure)
33 if (typeof options ===
'string') {
34 options = url.parse(options)
35 options.host = options.hostname
36 options.path = options.pathname
39 if (typeof options.url !==
'undefined') {
40 var parsedUrl = url.parse(options.url);
41 options.host = parsedUrl.hostname;
42 options.path = parsedUrl.pathname;
43 options.port = parsedUrl.port;
48 'User-Agent' :
'NodeJS XML-RPC Client'
49 ,
'Content-Type' :
'text/xml'
50 ,
'Accept' :
'text/xml'
51 ,
'Accept-Charset' :
'UTF8'
52 ,
'Connection' :
'Keep-Alive'
54 options.headers = options.headers || {}
56 if (options.headers.Authorization == null &&
57 options.basic_auth != null &&
58 options.basic_auth.user != null &&
59 options.basic_auth.pass != null)
61 var auth = options.basic_auth.user +
':' + options.basic_auth.pass
62 options.headers[
'Authorization'] =
'Basic ' +
new Buffer(auth).toString(
'base64')
65 for (var attribute in headers) {
66 if (options.headers[attribute] === undefined) {
67 options.headers[attribute] = headers[attribute]
71 options.method =
'POST'
72 this.options = options
74 this.isSecure = isSecure
75 this.headersProcessors = {
77 composeRequest:
function(headers) {
78 this.processors.forEach(
function(p) {p.composeRequest(headers);})
80 parseResponse:
function(headers) {
81 this.processors.forEach(
function(p) {p.parseResponse(headers);})
84 if (options.cookies) {
85 this.cookies =
new Cookies();
86 this.headersProcessors.processors.unshift(this.cookies);
99 Client.prototype.methodCall =
function methodCall(method, params, callback) {
100 var options = this.options
101 var xml = Serializer.serializeMethodCall(method, params, options.encoding)
102 var transport = this.isSecure ? https : http
104 options.headers[
'Content-Length'] = Buffer.byteLength(xml,
'utf8')
105 this.headersProcessors.composeRequest(options.headers)
106 var request = transport.request(options,
function(response) {
109 response.on(
'data',
function (chunk) { body.push(chunk) })
111 function __enrichError (err) {
112 Object.defineProperty(err,
'req', { value: request })
113 Object.defineProperty(err,
'res', { value: response })
114 Object.defineProperty(err,
'body', { value: body.join(
'') })
118 if (response.statusCode == 404) {
119 callback(__enrichError(
new Error(
'Not Found')))
122 this.headersProcessors.parseResponse(response.headers)
124 var deserializer =
new Deserializer(options.responseEncoding)
126 deserializer.deserializeMethodResponse(response, function(err, result) {
128 err = __enrichError(err)
130 callback(err, result)
135 request.on(
'error', callback)
136 request.write(xml,
'utf8')
147 Client.prototype.getCookie =
function getCookie(name) {
149 throw 'Cookies support is not turned on for this client instance';
151 return this.cookies.get(name);
168 Client.prototype.setCookie =
function setCookie(name, value) {
170 throw 'Cookies support is not turned on for this client instance';
172 this.cookies.set(name, value);
176 module.exports = Client