artdaq_node_server  v1_00_07
 All Classes Namespaces Files Variables Pages
client.js
1 var http = require('http')
2  , https = require('https')
3  , url = require('url')
4  , Serializer = require('./serializer')
5  , Deserializer = require('./deserializer')
6  , Cookies = require('./cookies')
7 
8 
25 function Client(options, isSecure) {
26 
27  // Invokes with new if called without
28  if (false === (this instanceof Client)) {
29  return new Client(options, isSecure)
30  }
31 
32  // If a string URI is passed in, converts to URI fields
33  if (typeof options === 'string') {
34  options = url.parse(options)
35  options.host = options.hostname
36  options.path = options.pathname
37  }
38 
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;
44  }
45 
46  // Set the HTTP request headers
47  var headers = {
48  'User-Agent' : 'NodeJS XML-RPC Client'
49  , 'Content-Type' : 'text/xml'
50  , 'Accept' : 'text/xml'
51  , 'Accept-Charset' : 'UTF8'
52  , 'Connection' : 'Keep-Alive'
53  }
54  options.headers = options.headers || {}
55 
56  if (options.headers.Authorization == null &&
57  options.basic_auth != null &&
58  options.basic_auth.user != null &&
59  options.basic_auth.pass != null)
60  {
61  var auth = options.basic_auth.user + ':' + options.basic_auth.pass
62  options.headers['Authorization'] = 'Basic ' + new Buffer(auth).toString('base64')
63  }
64 
65  for (var attribute in headers) {
66  if (options.headers[attribute] === undefined) {
67  options.headers[attribute] = headers[attribute]
68  }
69  }
70 
71  options.method = 'POST'
72  this.options = options
73 
74  this.isSecure = isSecure
75  this.headersProcessors = {
76  processors: [],
77  composeRequest: function(headers) {
78  this.processors.forEach(function(p) {p.composeRequest(headers);})
79  },
80  parseResponse: function(headers) {
81  this.processors.forEach(function(p) {p.parseResponse(headers);})
82  }
83  };
84  if (options.cookies) {
85  this.cookies = new Cookies();
86  this.headersProcessors.processors.unshift(this.cookies);
87  }
88 }
89 
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
103 
104  options.headers['Content-Length'] = Buffer.byteLength(xml, 'utf8')
105  this.headersProcessors.composeRequest(options.headers)
106  var request = transport.request(options, function(response) {
107 
108  var body = []
109  response.on('data', function (chunk) { body.push(chunk) })
110 
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('') })
115  return err
116  }
117 
118  if (response.statusCode == 404) {
119  callback(__enrichError(new Error('Not Found')))
120  }
121  else {
122  this.headersProcessors.parseResponse(response.headers)
123 
124  var deserializer = new Deserializer(options.responseEncoding)
125 
126  deserializer.deserializeMethodResponse(response, function(err, result) {
127  if (err) {
128  err = __enrichError(err)
129  }
130  callback(err, result)
131  })
132  }
133  }.bind(this))
134 
135  request.on('error', callback)
136  request.write(xml, 'utf8')
137  request.end()
138 }
139 
147 Client.prototype.getCookie = function getCookie(name) {
148  if (!this.cookies) {
149  throw 'Cookies support is not turned on for this client instance';
150  }
151  return this.cookies.get(name);
152 }
153 
168 Client.prototype.setCookie = function setCookie(name, value) {
169  if (!this.cookies) {
170  throw 'Cookies support is not turned on for this client instance';
171  }
172  this.cookies.set(name, value);
173  return this;
174 }
175 
176 module.exports = Client
177