Skip to content
This repository

Noserver fix #16

Merged
1 commit merged into from over 3 years ago

1 participant

Fedor Indutny
Fedor Indutny

No description provided.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Nov 03, 2010
Fedor Indutny indutny No-server fix f84880f
This page is out of date. Refresh to see the latest.

Showing 2 changed files with 165 additions and 12 deletions. Show diff stats Hide diff stats

  1. +25 12 lib/node-http-proxy.js
  2. +140 0 test/node-http-proxy-notarget.js
37 lib/node-http-proxy.js
@@ -160,21 +160,29 @@ HttpProxy.prototype = {
160 160 //p.request(req.method, req.url, req.headers, function (reverse_proxy) {
161 161
162 162 // Create an error handler so we can use it temporarily
163   - var error = function (err) {
164   - res.writeHead(500, {'Content-Type': 'text/plain'});
  163 + function error(obj) {
  164 + var fn = function (err) {
  165 + res.writeHead(500, {'Content-Type': 'text/plain'});
165 166
166   - if(req.method !== 'HEAD') {
167   - res.write('An error has occurred: ' + JSON.stringify(err));
168   - }
  167 + if(req.method !== 'HEAD') {
  168 + res.write('An error has occurred: ' + JSON.stringify(err));
  169 + }
  170 +
  171 + // Response end may never come so removeListener here
  172 + obj.removeListener('error', fn);
  173 + res.end();
  174 + };
169 175
170   - // Response end may never come so removeListener here
171   - reverse_proxy.removeListener('error', error);
172   - res.end();
  176 + return fn;
173 177 };
174 178
175 179 // Add a listener for the connection timeout event
176   - reverse_proxy.addListener('error', error);
177   -
  180 + var reverseProxyError = error(reverse_proxy),
  181 + clientError = error(client);
  182 +
  183 + reverse_proxy.addListener('error', reverseProxyError);
  184 + client.addListener('error', clientError);
  185 +
178 186 // Add a listener for the reverse_proxy response event
179 187 reverse_proxy.addListener('response', function (response) {
180 188 if (response.headers.connection) {
@@ -204,7 +212,7 @@ HttpProxy.prototype = {
204 212 response.addListener('end', function () {
205 213 // Remark: Emit the end event for testability
206 214 self.emitter.emit('proxy', null, self.body);
207   - reverse_proxy.removeListener('error', error);
  215 + reverse_proxy.removeListener('error', reverseProxyError);
208 216 res.end();
209 217 });
210 218 });
@@ -218,7 +226,12 @@ HttpProxy.prototype = {
218 226 req.addListener('end', function () {
219 227 reverse_proxy.end();
220 228 });
221   -
  229 +
  230 + // On 'close' event remove 'error' listener
  231 + client.addListener('close', function() {
  232 + client.removeListener('error', clientError);
  233 + });
  234 +
222 235 self.unwatch(req);
223 236
224 237 //});
140 test/node-http-proxy-notarget.js
... ... @@ -0,0 +1,140 @@
  1 +/*
  2 + node-http-proxy-test.js: http proxy for node.js
  3 +
  4 + Copyright (c) 2010 Charlie Robbins & Marak Squires http://github.com/nodejitsu/node-http-proxy
  5 +
  6 + Permission is hereby granted, free of charge, to any person obtaining
  7 + a copy of this software and associated documentation files (the
  8 + "Software"), to deal in the Software without restriction, including
  9 + without limitation the rights to use, copy, modify, merge, publish,
  10 + distribute, sublicense, and/or sell copies of the Software, and to
  11 + permit persons to whom the Software is furnished to do so, subject to
  12 + the following conditions:
  13 +
  14 + The above copyright notice and this permission notice shall be
  15 + included in all copies or substantial portions of the Software.
  16 +
  17 + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18 + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19 + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20 + NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  21 + LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  22 + OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23 + WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24 +
  25 +*/
  26 +
  27 +var vows = require('vows'),
  28 + sys = require('sys'),
  29 + assert = require('assert'),
  30 + http = require('http');
  31 +
  32 +var httpProxy = require('./../lib/node-http-proxy');
  33 +var testServers = {};
  34 +
  35 +//
  36 +// Creates the reverse proxy server
  37 +//
  38 +var startProxyServer = function (port, server) {
  39 + var proxyServer = httpProxy.createServer(port, server);
  40 + proxyServer.listen(8080);
  41 + return proxyServer;
  42 +};
  43 +
  44 +//
  45 +// Creates the reverse proxy server with a specified latency
  46 +//
  47 +var startLatentProxyServer = function (port, server, latency) {
  48 + // Initialize the nodeProxy and start proxying the request
  49 + var proxyServer = httpProxy.createServer(function (req, res, proxy) {
  50 + setTimeout(function () {
  51 + proxy.proxyRequest(port, server);
  52 + }, latency);
  53 + });
  54 +
  55 + proxyServer.listen(8081);
  56 + return proxyServer;
  57 +};
  58 +
  59 +//
  60 +// Creates the 'hellonode' server
  61 +//
  62 +var startTargetServer = function (port) {
  63 + var targetServer = http.createServer(function (req, res) {
  64 + res.writeHead(200, {'Content-Type': 'text/plain'});
  65 + res.write('hello world')
  66 + res.end();
  67 + });
  68 +
  69 + targetServer.listen(port);
  70 + return targetServer;
  71 +};
  72 +
  73 +//
  74 +// The default test bootstrapper with no latency
  75 +//
  76 +var startTest = function (port) {
  77 + var proxyServer = startProxyServer(port, 'localhost'),
  78 + targetServer = startTargetServer(port + 1000);
  79 +
  80 + testServers.noLatency = [];
  81 + testServers.noLatency.push(proxyServer);
  82 + testServers.noLatency.push(targetServer);
  83 +
  84 + return proxyServer;
  85 +};
  86 +
  87 +//
  88 +// The test bootstrapper with some latency
  89 +//
  90 +var startTestWithLatency = function (port) {
  91 + var proxyServer = startLatentProxyServer(port, 'localhost', 2000),
  92 + targetServer = startTargetServer(port);
  93 +
  94 + testServers.latency = [];
  95 + testServers.latency.push(proxyServer);
  96 + testServers.latency.push(targetServer);
  97 +
  98 + return proxyServer;
  99 +};
  100 +
  101 +vows.describe('node-http-proxy').addBatch({
  102 + "A node-http-proxy": {
  103 + "when instantiated directly": {
  104 + "and an incoming request is proxied to the helloNode server" : {
  105 + "with no latency" : {
  106 + topic: function () {
  107 + var proxy = startTest(8082);
  108 + proxy.addListener('proxy', this.callback);
  109 +
  110 + var client = http.createClient(8080, 'localhost');
  111 + var request = client.request('GET', '/');
  112 + request.end();
  113 + },
  114 + "it should received 'hello world'": function (err, body) {
  115 + assert.equal(body, 'hello world');
  116 + testServers.noLatency.forEach(function (server) {
  117 + server.close();
  118 + })
  119 + }
  120 + },
  121 + "with latency": {
  122 + topic: function () {
  123 + var proxy = startTestWithLatency(8083);
  124 + proxy.addListener('proxy', this.callback);
  125 +
  126 + var client = http.createClient(8081, 'localhost');
  127 + var request = client.request('GET', '/');
  128 + request.end();
  129 + },
  130 + "it should receive 'hello world'": function (err, body) {
  131 + assert.equal(body, 'hello world');
  132 + testServers.latency.forEach(function (server) {
  133 + server.close();
  134 + })
  135 + }
  136 + }
  137 + }
  138 + }
  139 + }
  140 +}).export(module);

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.