Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
N
node-http-proxy
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
node-http-proxy
Commits
f84880fc
Commit
f84880fc
authored
Nov 03, 2010
by
Fedor Indutny
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
No-server fix
parent
73381cf7
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
165 additions
and
12 deletions
+165
-12
lib/node-http-proxy.js
lib/node-http-proxy.js
+25
-12
test/node-http-proxy-notarget.js
test/node-http-proxy-notarget.js
+140
-0
No files found.
lib/node-http-proxy.js
View file @
f84880fc
...
...
@@ -160,21 +160,29 @@ HttpProxy.prototype = {
//p.request(req.method, req.url, req.headers, function (reverse_proxy) {
// Create an error handler so we can use it temporarily
var
error
=
function
(
err
)
{
res
.
writeHead
(
500
,
{
'
Content-Type
'
:
'
text/plain
'
});
function
error
(
obj
)
{
var
fn
=
function
(
err
)
{
res
.
writeHead
(
500
,
{
'
Content-Type
'
:
'
text/plain
'
});
if
(
req
.
method
!==
'
HEAD
'
)
{
res
.
write
(
'
An error has occurred:
'
+
JSON
.
stringify
(
err
));
}
if
(
req
.
method
!==
'
HEAD
'
)
{
res
.
write
(
'
An error has occurred:
'
+
JSON
.
stringify
(
err
));
}
// Response end may never come so removeListener here
obj
.
removeListener
(
'
error
'
,
fn
);
res
.
end
();
};
// Response end may never come so removeListener here
reverse_proxy
.
removeListener
(
'
error
'
,
error
);
res
.
end
();
return
fn
;
};
// Add a listener for the connection timeout event
reverse_proxy
.
addListener
(
'
error
'
,
error
);
var
reverseProxyError
=
error
(
reverse_proxy
),
clientError
=
error
(
client
);
reverse_proxy
.
addListener
(
'
error
'
,
reverseProxyError
);
client
.
addListener
(
'
error
'
,
clientError
);
// Add a listener for the reverse_proxy response event
reverse_proxy
.
addListener
(
'
response
'
,
function
(
response
)
{
if
(
response
.
headers
.
connection
)
{
...
...
@@ -204,7 +212,7 @@ HttpProxy.prototype = {
response
.
addListener
(
'
end
'
,
function
()
{
// Remark: Emit the end event for testability
self
.
emitter
.
emit
(
'
proxy
'
,
null
,
self
.
body
);
reverse_proxy
.
removeListener
(
'
error
'
,
e
rror
);
reverse_proxy
.
removeListener
(
'
error
'
,
reverseProxyE
rror
);
res
.
end
();
});
});
...
...
@@ -218,7 +226,12 @@ HttpProxy.prototype = {
req
.
addListener
(
'
end
'
,
function
()
{
reverse_proxy
.
end
();
});
// On 'close' event remove 'error' listener
client
.
addListener
(
'
close
'
,
function
()
{
client
.
removeListener
(
'
error
'
,
clientError
);
});
self
.
unwatch
(
req
);
//});
...
...
test/node-http-proxy-notarget.js
0 → 100644
View file @
f84880fc
/*
node-http-proxy-test.js: http proxy for node.js
Copyright (c) 2010 Charlie Robbins & Marak Squires http://github.com/nodejitsu/node-http-proxy
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
var
vows
=
require
(
'
vows
'
),
sys
=
require
(
'
sys
'
),
assert
=
require
(
'
assert
'
),
http
=
require
(
'
http
'
);
var
httpProxy
=
require
(
'
./../lib/node-http-proxy
'
);
var
testServers
=
{};
//
// Creates the reverse proxy server
//
var
startProxyServer
=
function
(
port
,
server
)
{
var
proxyServer
=
httpProxy
.
createServer
(
port
,
server
);
proxyServer
.
listen
(
8080
);
return
proxyServer
;
};
//
// Creates the reverse proxy server with a specified latency
//
var
startLatentProxyServer
=
function
(
port
,
server
,
latency
)
{
// Initialize the nodeProxy and start proxying the request
var
proxyServer
=
httpProxy
.
createServer
(
function
(
req
,
res
,
proxy
)
{
setTimeout
(
function
()
{
proxy
.
proxyRequest
(
port
,
server
);
},
latency
);
});
proxyServer
.
listen
(
8081
);
return
proxyServer
;
};
//
// Creates the 'hellonode' server
//
var
startTargetServer
=
function
(
port
)
{
var
targetServer
=
http
.
createServer
(
function
(
req
,
res
)
{
res
.
writeHead
(
200
,
{
'
Content-Type
'
:
'
text/plain
'
});
res
.
write
(
'
hello world
'
)
res
.
end
();
});
targetServer
.
listen
(
port
);
return
targetServer
;
};
//
// The default test bootstrapper with no latency
//
var
startTest
=
function
(
port
)
{
var
proxyServer
=
startProxyServer
(
port
,
'
localhost
'
),
targetServer
=
startTargetServer
(
port
+
1000
);
testServers
.
noLatency
=
[];
testServers
.
noLatency
.
push
(
proxyServer
);
testServers
.
noLatency
.
push
(
targetServer
);
return
proxyServer
;
};
//
// The test bootstrapper with some latency
//
var
startTestWithLatency
=
function
(
port
)
{
var
proxyServer
=
startLatentProxyServer
(
port
,
'
localhost
'
,
2000
),
targetServer
=
startTargetServer
(
port
);
testServers
.
latency
=
[];
testServers
.
latency
.
push
(
proxyServer
);
testServers
.
latency
.
push
(
targetServer
);
return
proxyServer
;
};
vows
.
describe
(
'
node-http-proxy
'
).
addBatch
({
"
A node-http-proxy
"
:
{
"
when instantiated directly
"
:
{
"
and an incoming request is proxied to the helloNode server
"
:
{
"
with no latency
"
:
{
topic
:
function
()
{
var
proxy
=
startTest
(
8082
);
proxy
.
addListener
(
'
proxy
'
,
this
.
callback
);
var
client
=
http
.
createClient
(
8080
,
'
localhost
'
);
var
request
=
client
.
request
(
'
GET
'
,
'
/
'
);
request
.
end
();
},
"
it should received 'hello world'
"
:
function
(
err
,
body
)
{
assert
.
equal
(
body
,
'
hello world
'
);
testServers
.
noLatency
.
forEach
(
function
(
server
)
{
server
.
close
();
})
}
},
"
with latency
"
:
{
topic
:
function
()
{
var
proxy
=
startTestWithLatency
(
8083
);
proxy
.
addListener
(
'
proxy
'
,
this
.
callback
);
var
client
=
http
.
createClient
(
8081
,
'
localhost
'
);
var
request
=
client
.
request
(
'
GET
'
,
'
/
'
);
request
.
end
();
},
"
it should receive 'hello world'
"
:
function
(
err
,
body
)
{
assert
.
equal
(
body
,
'
hello world
'
);
testServers
.
latency
.
forEach
(
function
(
server
)
{
server
.
close
();
})
}
}
}
}
}
}).
export
(
module
);
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment