Skip to content

Commit 2ce1552

Browse files
committed
Merge pull request justmoon#7 from dcharbonnier/develop
Allow custom headers
2 parents e942c3a + b82b399 commit 2ce1552

2 files changed

Lines changed: 17 additions & 10 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ var rpc = require('json-rpc2');
2525

2626
var server = rpc.Server.create({
2727
'websocket': true // is true by default
28+
'headers': { // allow custom headers is empty by default
29+
'Access-Control-Allow-Origin': '*'
30+
}
2831
});
2932

3033
function add(args, opt, callback) {

src/server.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module.exports = function (classes) {
44
var
55
net = require('net'),
66
http = require('http'),
7+
extend = require('util')._extend,
78
JsonParser = require('jsonparse'),
89

910
UNAUTHORIZED = 'Unauthorized',
@@ -23,6 +24,7 @@ module.exports = function (classes) {
2324

2425
this.opts = opts || {};
2526
this.opts.type = typeof this.opts.type !== 'undefined' ? this.opts.type : 'http';
27+
this.opts.headers = this.opts.headers || {};
2628
this.opts.websocket = typeof this.opts.websocket !== 'undefined' ? this.opts.websocket : true;
2729
},
2830
_checkAuth: function (req, res) {
@@ -40,7 +42,7 @@ module.exports = function (classes) {
4042
if (!this.authHandler(username, password)) {
4143
if (res) {
4244
classes.EventEmitter.trace('<--', 'Unauthorized request');
43-
Server.handleHttpError(req, res, new Error.InvalidParams(UNAUTHORIZED));
45+
Server.handleHttpError(req, res, new Error.InvalidParams(UNAUTHORIZED), self.opts.headers);
4446
}
4547
return false;
4648
}
@@ -122,7 +124,7 @@ module.exports = function (classes) {
122124
Endpoint.trace('<--', 'Accepted http request');
123125

124126
if (req.method !== 'POST') {
125-
Server.handleHttpError(req, res, new Error.InvalidRequest(METHOD_NOT_ALLOWED));
127+
Server.handleHttpError(req, res, new Error.InvalidRequest(METHOD_NOT_ALLOWED), self.opts.headers);
126128
return;
127129
}
128130

@@ -133,28 +135,28 @@ module.exports = function (classes) {
133135
try {
134136
decoded = JSON.parse(buf);
135137
} catch (error) {
136-
Server.handleHttpError(req, res, new Error.ParseError(INVALID_REQUEST));
138+
Server.handleHttpError(req, res, new Error.ParseError(INVALID_REQUEST), self.opts.headers);
137139
return;
138140
}
139141

140142
// Check for the required fields, and if they aren't there, then
141143
// dispatch to the handleHttpError function.
142144
if (!(decoded.method && decoded.params && decoded.id)) {
143145
Endpoint.trace('-->', 'Response (invalid request)');
144-
Server.handleHttpError(req, res, new Error.InvalidRequest(INVALID_REQUEST));
146+
Server.handleHttpError(req, res, new Error.InvalidRequest(INVALID_REQUEST), self.opts.headers);
145147
return;
146148
}
147149

148150
var reply = function reply(json) {
149151
var encoded = JSON.stringify(json);
150152

151153
if (!conn.isStreaming) {
152-
res.writeHead(200, {'Content-Type': 'application/json',
153-
'Content-Length': Buffer.byteLength(encoded, 'utf-8')});
154+
res.writeHead(200, extend(self.opts.headers, {'Content-Type': 'application/json',
155+
'Content-Length': Buffer.byteLength(encoded, 'utf-8')}));
154156
res.write(encoded);
155157
res.end();
156158
} else {
157-
res.writeHead(200, {'Content-Type': 'application/json'});
159+
res.writeHead(200, extend(self.opts.headers, {'Content-Type': 'application/json'}));
158160
res.write(encoded);
159161
// Keep connection open
160162
}
@@ -338,15 +340,17 @@ module.exports = function (classes) {
338340
/**
339341
* Handle a low level server error.
340342
*/
341-
handleHttpError: function (req, res, error) {
343+
handleHttpError: function (req, res, error, custom_headers) {
342344
var message = JSON.stringify({
343345
'jsonrpc': '2.0',
344346
'error': {code: error.code, message: error.message},
345347
'id': null
346348
});
347-
var headers = {'Content-Type': 'application/json',
349+
custom_headers = custom_headers || {};
350+
var headers = extend(custom_headers, {'Content-Type': 'application/json',
348351
'Content-Length': Buffer.byteLength(message),
349-
'Allow': 'POST'};
352+
'Access-Control-Allow-Headers': 'Content-Type',
353+
'Allow': 'POST'});
350354

351355
/*if (code === 401) {
352356
headers['WWW-Authenticate'] = 'Basic realm=' + 'JSON-RPC' + '';

0 commit comments

Comments
 (0)