@@ -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