Proxy Server Support #107

Merged
merged 1 commit into from Dec 14, 2012
Jump to file
+14 −16
Split
View
@@ -12,7 +12,6 @@ var url = require('url'),
querystring = require('querystring'),
_ = require('underscore');
-
var STATUS_MSGS = {
400: '400: Bad Request',
401: '401: Unauthorized',
@@ -142,7 +141,7 @@ CouchDB.prototype.deleteDB = function (callback) {
CouchDB.prototype.client = function (method, path, data, callback) {
var pathname = this.instance.pathname;
- path = (pathname ? pathname + '/' + path: '/' + path);
+ path = (pathname ? pathname + '/' + path : '/' + path);
path = path.replace(/^\/\/+/, '/');
var headers = {
@@ -177,35 +176,35 @@ CouchDB.prototype.client = function (method, path, data, callback) {
headers.Authorization = "Basic " + enc;
}
- var proto = (this.instance.protocol === 'https:') ? https: http;
+ var proto = (this.instance.protocol === 'https:') ? https : http;
var proxy = process.env[ "HTTP_PROXY" ] || process.env[ "http_proxy" ];
var params = {
host: this.instance.hostname,
port: this.instance.port,
method: method,
- path: path,
+ path: proxy ? url.resolve(this.instance.href, path) : path,
headers: headers
};
- if ( proxy ) {
- var proxyUrl = url.parse( proxy );
+ if (proxy) {
+ var proxyUrl = url.parse(proxy);
params.headers = {
Host: params.host,
Port: params.port
};
params.host = proxyUrl.hostname;
params.port = proxyUrl.port;
- logger.info( "proxy", proxyUrl.hostname + ":" + proxyUrl.port );
+ logger.info("proxy", proxyUrl.hostname + ":" + proxyUrl.port);
- if ( proxyUrl.auth !== undefined ) {
+ if (proxyUrl.auth !== undefined) {
params.headers[ "Proxy-Authorization" ] = "Basic " + new Buffer(
- decodeURIComponent( proxyUrl.auth )
+ decodeURIComponent(proxyUrl.auth)
).toString('base64');
}
}
- var request = proto.request( params );
+ var request = proto.request(params);
logger.debug('request', method + ' ' + path);
logger.debug('request headers', headers);
@@ -231,7 +230,7 @@ CouchDB.prototype.client = function (method, path, data, callback) {
});
response.on('end', function () {
try {
- var data = buffer.length ? JSON.parse(buffer.join('')): null;
+ var data = buffer.length ? JSON.parse(buffer.join('')) : null;
}
catch (e) {
logger.info('Unexpected response', buffer.join(''));
@@ -240,7 +239,7 @@ CouchDB.prototype.client = function (method, path, data, callback) {
if (response.statusCode >= 300) {
if (data && data.error) {
var err = new Error(
- data.error + (data.reason ? '\n' + data.reason: '')
+ data.error + (data.reason ? '\n' + data.reason : '')
);
err.error = data.error;
err.reason = data.reason;
@@ -313,7 +312,7 @@ CouchDB.prototype.exists = function (id, callback) {
}
var exists = (res.statusCode === 200);
var etag = res.headers.etag;
- var _rev = etag ? etag.substr(1, etag.length - 2): null;
+ var _rev = etag ? etag.substr(1, etag.length - 2) : null;
callback(null, exists, _rev);
});
};
@@ -356,7 +355,7 @@ CouchDB.prototype.save = function (id, doc, /*optional*/options, callback) {
callback = options;
options = {};
}
- var method = id ? 'PUT': 'POST';
+ var method = id ? 'PUT' : 'POST';
var path = exports.encode(id || '');
if (options.force && id) {
@@ -450,7 +449,7 @@ CouchDB.prototype.session = function (callback) {
CouchDB.prototype.instanceURL = function (dburl) {
dburl = dburl || _.clone(this.instance);
- var parsed = (typeof dburl === "object") ? dburl: url.parse(dburl);
+ var parsed = (typeof dburl === "object") ? dburl : url.parse(dburl);
delete parsed.pathname;
delete parsed.query;
delete parsed.search;
@@ -488,4 +487,3 @@ exports.replicate = function (replicator, source, target, options, callback) {
}
});
};
-