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
f7010e51
Commit
f7010e51
authored
Sep 10, 2011
by
indexzero
Browse files
Options
Browse Files
Download
Plain Diff
[merge] Merge from significant internal refactor in v0.7.x. No external API changes
parents
38315f6b
0182ba37
Changes
32
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
32 changed files
with
1598 additions
and
1099 deletions
+1598
-1099
.gitignore
.gitignore
+1
-1
README.md
README.md
+8
-1
bin/node-http-proxy
bin/node-http-proxy
+1
-1
examples/helpers/store.js
examples/helpers/store.js
+3
-6
examples/http/basic-proxy.js
examples/http/basic-proxy.js
+11
-10
examples/http/concurrent-proxy.js
examples/http/concurrent-proxy.js
+12
-11
examples/http/custom-proxy-error.js
examples/http/custom-proxy-error.js
+2
-7
examples/http/forward-proxy.js
examples/http/forward-proxy.js
+2
-2
examples/http/latent-proxy.js
examples/http/latent-proxy.js
+2
-2
examples/http/proxy-https-to-http.js
examples/http/proxy-https-to-http.js
+1
-1
examples/http/proxy-https-to-https.js
examples/http/proxy-https-to-https.js
+1
-1
examples/http/proxy-table.js
examples/http/proxy-table.js
+1
-1
examples/http/standalone-proxy.js
examples/http/standalone-proxy.js
+2
-2
examples/middleware/bodyDecoder-middleware.js
examples/middleware/bodyDecoder-middleware.js
+0
-0
examples/middleware/gzip-middleware.js
examples/middleware/gzip-middleware.js
+1
-1
examples/middleware/jsonp-middleware.js
examples/middleware/jsonp-middleware.js
+1
-1
examples/middleware/url-middleware.js
examples/middleware/url-middleware.js
+1
-1
examples/middleware/url-middleware2.js
examples/middleware/url-middleware2.js
+5
-6
examples/package.json
examples/package.json
+9
-9
examples/websocket/latent-websocket-proxy.js
examples/websocket/latent-websocket-proxy.js
+2
-2
examples/websocket/standalone-websocket-proxy.js
examples/websocket/standalone-websocket-proxy.js
+1
-1
examples/websocket/websocket-proxy.js
examples/websocket/websocket-proxy.js
+1
-1
lib/node-http-proxy.js
lib/node-http-proxy.js
+223
-862
lib/node-http-proxy/http-proxy.js
lib/node-http-proxy/http-proxy.js
+766
-0
lib/node-http-proxy/proxy-table.js
lib/node-http-proxy/proxy-table.js
+37
-28
lib/node-http-proxy/routing-proxy.js
lib/node-http-proxy/routing-proxy.js
+247
-0
package.json
package.json
+8
-3
test/helpers.js
test/helpers.js
+114
-60
test/http/http-proxy-test.js
test/http/http-proxy-test.js
+5
-6
test/http/routing-proxy-test.js
test/http/routing-proxy-test.js
+12
-14
test/websocket/websocket-proxy-test.js
test/websocket/websocket-proxy-test.js
+14
-58
test/websocket/websocket-routing-proxy-test.js
test/websocket/websocket-routing-proxy-test.js
+104
-0
No files found.
.gitignore
View file @
f7010e51
test/
config.json
config.json
node_modules/
npm-debug.log
README.md
View file @
f7010e51
...
...
@@ -114,7 +114,7 @@ httpProxy.createServer(function (req, res, proxy) {
// Buffer the request so that `data` and `end` events
// are not lost during async operation(s).
//
var
buffer
=
p
roxy
.
buffer
(
req
);
var
buffer
=
httpP
roxy
.
buffer
(
req
);
//
// Wait for two seconds then respond: this simulates
...
...
@@ -357,6 +357,13 @@ server.on('upgrade', function(req, socket, head) {
});
```
### Configuring your Socket limits
By default,
`node-http-proxy`
will set a 100 socket limit for all
`host:port`
proxy targets. If you wish to change this you can two it in two ways:
1.
By passing the
`maxSockets`
option to
`httpProxy.createServer()`
2.
By calling
`httpProxy.setMaxSockets(n)`
, where
`n`
is the number of sockets you with to use.
## Using node-http-proxy from the command line
When you install this package with npm, a node-http-proxy binary will become available to you. Using this binary is easy with some simple options:
...
...
bin/node-http-proxy
View file @
f7010e51
...
...
@@ -4,7 +4,7 @@ var path = require('path'),
fs
=
require
(
'
fs
'
),
util
=
require
(
'
util
'
),
argv
=
require
(
'
optimist
'
).
argv
,
httpProxy
=
require
(
'
.
/.
./lib/node-http-proxy
'
);
httpProxy
=
require
(
'
../lib/node-http-proxy
'
);
var
help
=
[
"
usage: node-http-proxy [options]
"
,
...
...
examples/
lib
/store.js
→
examples/
helpers
/store.js
View file @
f7010e51
module
.
exports
=
Store
//
// just to make these example a little bit interesting,
// make a little key value store with an http interface
...
...
@@ -20,12 +19,10 @@ module.exports = Store
//
// TODO: cached map-reduce views and auto-magic sharding.
//
var
Store
=
module
.
exports
=
function
Store
()
{
this
.
store
=
{};
};
function
Store
()
{
this
.
store
=
{}
}
Store
.
prototype
=
{
get
:
function
(
key
)
{
return
this
.
store
[
key
]
...
...
examples/basic-proxy.js
→
examples/
http/
basic-proxy.js
View file @
f7010e51
...
...
@@ -27,16 +27,17 @@
var
util
=
require
(
'
util
'
),
colors
=
require
(
'
colors
'
),
http
=
require
(
'
http
'
),
httpProxy
=
require
(
'
./../lib/node-http-proxy
'
);
// ascii art from http://github.com/marak/asciimo
var
welcome
=
'
\
# # ##### ##### ##### ##### ##### #### # # # #
\n\
# # # # # # # # # # # # # # # #
\n\
###### # # # # ##### # # # # # # ## #
\n\
# # # # ##### ##### ##### # # ## #
\n\
# # # # # # # # # # # # #
\n\
# # # # # # # # #### # # #
\n
'
;
httpProxy
=
require
(
'
../../lib/node-http-proxy
'
);
var
welcome
=
[
'
# # ##### ##### ##### ##### ##### #### # # # #
'
,
'
# # # # # # # # # # # # # # # #
'
,
'
###### # # # # ##### # # # # # # ## #
'
,
'
# # # # ##### ##### ##### # # ## #
'
,
'
# # # # # # # # # # # # #
'
,
'
# # # # # # # # #### # # #
'
].
join
(
'
\n
'
);
util
.
puts
(
welcome
.
rainbow
.
bold
);
//
...
...
examples/concurrent-proxy.js
→
examples/
http/
concurrent-proxy.js
View file @
f7010e51
...
...
@@ -27,7 +27,7 @@
var
util
=
require
(
'
util
'
),
colors
=
require
(
'
colors
'
),
http
=
require
(
'
http
'
),
httpProxy
=
require
(
'
./../lib/node-http-proxy
'
);
httpProxy
=
require
(
'
.
.
/../lib/node-http-proxy
'
);
//
// Basic Http Proxy Server
...
...
@@ -42,23 +42,24 @@ httpProxy.createServer(9000, 'localhost').listen(8000);
//
var
connections
=
[]
,
go
var
connections
=
[]
,
go
;
http
.
createServer
(
function
(
req
,
res
)
{
connections
.
push
(
function
(){
connections
.
push
(
function
()
{
res
.
writeHead
(
200
,
{
'
Content-Type
'
:
'
text/plain
'
});
res
.
write
(
'
request successfully proxied to:
'
+
req
.
url
+
'
\n
'
+
JSON
.
stringify
(
req
.
headers
,
true
,
2
));
res
.
end
();
})
process
.
stdout
.
write
(
connections
.
length
+
'
,
'
)
});
process
.
stdout
.
write
(
connections
.
length
+
'
,
'
);
if
(
connections
.
length
>
110
||
go
)
{
go
=
true
while
(
connections
.
length
)
connections
.
shift
()()
go
=
true
;
while
(
connections
.
length
)
{
connections
.
shift
()();
}
}
}).
listen
(
9000
);
util
.
puts
(
'
http proxy server
'
.
blue
+
'
started
'
.
green
.
bold
+
'
on port
'
.
blue
+
'
8000
'
.
yellow
);
...
...
examples/custom-proxy-error.js
→
examples/
http/
custom-proxy-error.js
View file @
f7010e51
...
...
@@ -27,17 +27,12 @@
var
util
=
require
(
'
util
'
),
colors
=
require
(
'
colors
'
),
http
=
require
(
'
http
'
),
httpProxy
=
require
(
'
./../lib/node-http-proxy
'
);
httpProxy
=
require
(
'
.
.
/../lib/node-http-proxy
'
);
//
// Http Proxy Server with Latency
//
var
server
=
httpProxy
.
createServer
(
function
(
req
,
res
,
proxy
)
{
proxy
.
proxyRequest
(
req
,
res
,
{
port
:
9000
,
host
:
'
localhost
'
});
})
var
server
=
httpProxy
.
createServer
(
9000
,
'
localhost
'
);
//
// Tell the server to listen on port 8002
...
...
examples/forward-proxy.js
→
examples/
http/
forward-proxy.js
View file @
f7010e51
...
...
@@ -27,7 +27,7 @@
var
util
=
require
(
'
util
'
),
colors
=
require
(
'
colors
'
),
http
=
require
(
'
http
'
),
httpProxy
=
require
(
'
./../lib/node-http-proxy
'
);
httpProxy
=
require
(
'
.
.
/../lib/node-http-proxy
'
);
//
// Setup proxy server with forwarding
...
...
examples/latent-proxy.js
→
examples/
http/
latent-proxy.js
View file @
f7010e51
...
...
@@ -27,13 +27,13 @@
var
util
=
require
(
'
util
'
),
colors
=
require
(
'
colors
'
),
http
=
require
(
'
http
'
),
httpProxy
=
require
(
'
./../lib/node-http-proxy
'
);
httpProxy
=
require
(
'
.
.
/../lib/node-http-proxy
'
);
//
// Http Proxy Server with Latency
//
httpProxy
.
createServer
(
function
(
req
,
res
,
proxy
)
{
var
buffer
=
p
roxy
.
buffer
(
req
);
var
buffer
=
httpP
roxy
.
buffer
(
req
);
setTimeout
(
function
()
{
proxy
.
proxyRequest
(
req
,
res
,
{
port
:
9000
,
...
...
examples/proxy-https-to-http.js
→
examples/
http/
proxy-https-to-http.js
View file @
f7010e51
...
...
@@ -28,7 +28,7 @@ var https = require('https'),
http
=
require
(
'
http
'
),
util
=
require
(
'
util
'
),
colors
=
require
(
'
colors
'
),
httpProxy
=
require
(
'
./../lib/node-http-proxy
'
),
httpProxy
=
require
(
'
.
.
/../lib/node-http-proxy
'
),
helpers
=
require
(
'
./../test/helpers
'
);
var
opts
=
helpers
.
loadHttps
();
...
...
examples/proxy-https-to-https.js
→
examples/
http/
proxy-https-to-https.js
View file @
f7010e51
...
...
@@ -28,7 +28,7 @@ var https = require('https'),
http
=
require
(
'
http
'
),
util
=
require
(
'
util
'
),
colors
=
require
(
'
colors
'
),
httpProxy
=
require
(
'
./../lib/node-http-proxy
'
),
httpProxy
=
require
(
'
.
.
/../lib/node-http-proxy
'
),
helpers
=
require
(
'
./../test/helpers
'
);
var
opts
=
helpers
.
loadHttps
();
...
...
examples/proxy-table.js
→
examples/
http/
proxy-table.js
View file @
f7010e51
...
...
@@ -27,7 +27,7 @@
var
util
=
require
(
'
util
'
),
colors
=
require
(
'
colors
'
),
http
=
require
(
'
http
'
),
httpProxy
=
require
(
'
./../lib/node-http-proxy
'
);
httpProxy
=
require
(
'
.
.
/../lib/node-http-proxy
'
);
//
// Http Proxy Server with Proxy Table
...
...
examples/standalone-proxy.js
→
examples/
http/
standalone-proxy.js
View file @
f7010e51
...
...
@@ -27,14 +27,14 @@
var
util
=
require
(
'
util
'
),
colors
=
require
(
'
colors
'
),
http
=
require
(
'
http
'
),
httpProxy
=
require
(
'
./../lib/node-http-proxy
'
);
httpProxy
=
require
(
'
.
.
/../lib/node-http-proxy
'
);
//
// Http Server with proxyRequest Handler and Latency
//
var
proxy
=
new
httpProxy
.
HttpProxy
();
http
.
createServer
(
function
(
req
,
res
)
{
var
buffer
=
p
roxy
.
buffer
(
req
);
var
buffer
=
httpP
roxy
.
buffer
(
req
);
setTimeout
(
function
()
{
proxy
.
proxyRequest
(
req
,
res
,
{
port
:
9000
,
...
...
examples/bodyDecoder-middleware.js
→
examples/
middleware/
bodyDecoder-middleware.js
View file @
f7010e51
File moved
examples/gzip-middleware.js
→
examples/
middleware/
gzip-middleware.js
View file @
f7010e51
...
...
@@ -27,7 +27,7 @@
var
util
=
require
(
'
util
'
),
colors
=
require
(
'
colors
'
),
http
=
require
(
'
http
'
),
httpProxy
=
require
(
'
./../lib/node-http-proxy
'
);
httpProxy
=
require
(
'
.
.
/../lib/node-http-proxy
'
);
//
// Basic Http Proxy Server
...
...
examples/jsonp-middleware.js
→
examples/
middleware/
jsonp-middleware.js
View file @
f7010e51
examples/url-middleware.js
→
examples/
middleware/
url-middleware.js
View file @
f7010e51
examples/url-middleware2.js
→
examples/
middleware/
url-middleware2.js
View file @
f7010e51
...
...
@@ -19,12 +19,11 @@ httpProxy.createServer(
//
// Target Http Server (to listen for requests on 'localhost')
//
http
.
createServer
(
function
(
req
,
res
)
{
http
.
createServer
(
function
(
req
,
res
)
{
res
.
writeHead
(
200
,
{
'
Content-Type
'
:
'
text/plain
'
});
res
.
write
(
'
request successfully proxied to:
'
+
req
.
url
+
'
\n
'
+
JSON
.
stringify
(
req
.
headers
,
true
,
2
));
res
.
end
();
}).
listen
(
9000
);
}).
listen
(
9000
);
// And finally, some colored startup output.
util
.
puts
(
'
http proxy server
'
.
blue
+
'
started
'
.
green
.
bold
+
'
on port
'
.
blue
+
'
8000
'
.
yellow
);
...
...
examples/package.json
View file @
f7010e51
{
"name"
:
"http-proxy-examples"
,
"description"
:
"packages required to run the examples"
,
"version"
:
"0.0.0"
,
"dependencies"
:
{
"connect"
:
"1.6"
,
"connect-gzip"
:
"0.1"
,
"connect-jsonp"
:
"0.0.5"
,
"connect-restreamer"
:
"1"
,
"proxy-by-url"
:
">= 0.0.1"
"name"
:
"http-proxy-examples"
,
"description"
:
"packages required to run the examples"
,
"version"
:
"0.0.0"
,
"dependencies"
:
{
"connect"
:
"1.6"
,
"connect-gzip"
:
"0.1"
,
"connect-jsonp"
:
"0.0.5"
,
"connect-restreamer"
:
"1"
,
"proxy-by-url"
:
">= 0.0.1"
}
}
\ No newline at end of file
examples/latent-websocket-proxy.js
→
examples/
websocket/
latent-websocket-proxy.js
View file @
f7010e51
...
...
@@ -28,7 +28,7 @@ var sys = require('sys'),
http
=
require
(
'
http
'
),
colors
=
require
(
'
colors
'
),
websocket
=
require
(
'
./../vendor/websocket
'
),
httpProxy
=
require
(
'
./../lib/node-http-proxy
'
);
httpProxy
=
require
(
'
.
.
/../lib/node-http-proxy
'
);
try
{
var
utils
=
require
(
'
socket.io/lib/socket.io/utils
'
),
...
...
@@ -80,7 +80,7 @@ var proxyServer = http.createServer(function (req, res) {
// WebSocket requests as well.
//
proxyServer
.
on
(
'
upgrade
'
,
function
(
req
,
socket
,
head
)
{
var
buffer
=
p
roxy
.
buffer
(
socket
);
var
buffer
=
httpP
roxy
.
buffer
(
socket
);
setTimeout
(
function
()
{
proxy
.
proxyWebSocketRequest
(
req
,
socket
,
head
,
{
...
...
examples/standalone-websocket-proxy.js
→
examples/
websocket/
standalone-websocket-proxy.js
View file @
f7010e51
...
...
@@ -28,7 +28,7 @@ var sys = require('sys'),
http
=
require
(
'
http
'
),
colors
=
require
(
'
colors
'
),
websocket
=
require
(
'
./../vendor/websocket
'
),
httpProxy
=
require
(
'
./../lib/node-http-proxy
'
);
httpProxy
=
require
(
'
.
.
/../lib/node-http-proxy
'
);
try
{
var
utils
=
require
(
'
socket.io/lib/socket.io/utils
'
),
...
...
examples/web
-
socket-proxy.js
→
examples/web
socket/web
socket-proxy.js
View file @
f7010e51
...
...
@@ -28,7 +28,7 @@ var sys = require('sys'),
http
=
require
(
'
http
'
),
colors
=
require
(
'
colors
'
),
websocket
=
require
(
'
./../vendor/websocket
'
),
httpProxy
=
require
(
'
./../lib/node-http-proxy
'
);
httpProxy
=
require
(
'
.
.
/../lib/node-http-proxy
'
);
try
{
var
utils
=
require
(
'
socket.io/lib/socket.io/utils
'
),
...
...
lib/node-http-proxy.js
View file @
f7010e51
This diff is collapsed.
Click to expand it.
lib/node-http-proxy/http-proxy.js
0 → 100644
View file @
f7010e51
This diff is collapsed.
Click to expand it.
lib/proxy-table.js
→
lib/
node-http-proxy/
proxy-table.js
View file @
f7010e51
...
...
@@ -37,31 +37,34 @@ var util = require('util'),
// locations of proxy targets based on ServerRequest headers; specifically
// the HTTP host header.
//
var
ProxyTable
=
exports
.
ProxyTable
=
function
(
router
,
silent
,
hostnameOnly
)
{
var
ProxyTable
=
exports
.
ProxyTable
=
function
(
options
)
{
events
.
EventEmitter
.
call
(
this
);
this
.
silent
=
typeof
silent
!==
'
undefined
'
?
silent
:
true
;
this
.
hostnameOnly
=
typeof
hostnameOnly
!==
'
undefined
'
?
hostnameOnly
:
fals
e
;
this
.
silent
=
options
.
silent
||
options
.
silent
!==
true
;
this
.
hostnameOnly
=
options
.
hostnameOnly
===
tru
e
;
if
(
typeof
router
===
'
object
'
)
{
if
(
typeof
options
.
router
===
'
object
'
)
{
//
// If we are passed an object literal setup
// the routes with RegExps from the router
//
this
.
setRoutes
(
router
);
this
.
setRoutes
(
options
.
router
);
}
else
if
(
typeof
router
===
'
string
'
)
{
else
if
(
typeof
options
.
router
===
'
string
'
)
{
//
// If we are passed a string then assume it is a
// file path, parse that file and watch it for changes
//
var
self
=
this
;
this
.
routeFile
=
router
;
this
.
setRoutes
(
JSON
.
parse
(
fs
.
readFileSync
(
router
)).
router
);
this
.
routeFile
=
options
.
router
;
this
.
setRoutes
(
JSON
.
parse
(
fs
.
readFileSync
(
options
.
router
)).
router
);
fs
.
watchFile
(
this
.
routeFile
,
function
()
{
fs
.
readFile
(
self
.
routeFile
,
function
(
err
,
data
)
{
if
(
err
)
throw
err
;
if
(
err
)
{
self
.
emit
(
'
error
'
,
err
);
}
self
.
setRoutes
(
JSON
.
parse
(
data
).
router
);
self
.
emit
(
'
routes
'
,
self
.
hostnameOnly
===
false
?
self
.
routes
:
self
.
router
);
});
...
...
@@ -72,7 +75,9 @@ var ProxyTable = exports.ProxyTable = function (router, silent, hostnameOnly) {
}
};
// Inherit from events.EventEmitter
//
// Inherit from `events.EventEmitter`
//
util
.
inherits
(
ProxyTable
,
events
.
EventEmitter
);
//
...
...
@@ -81,7 +86,9 @@ util.inherits(ProxyTable, events.EventEmitter);
// Sets the host-based routes to be used by this instance.
//
ProxyTable
.
prototype
.
setRoutes
=
function
(
router
)
{
if
(
!
router
)
throw
new
Error
(
'
Cannot update ProxyTable routes without router.
'
);
if
(
!
router
)
{
throw
new
Error
(
'
Cannot update ProxyTable routes without router.
'
);
}
this
.
router
=
router
;
...
...
@@ -127,7 +134,9 @@ ProxyTable.prototype.getProxyLocation = function (req) {
else
{
target
+=
req
.
url
;
for
(
var
i
in
this
.
routes
)
{
var
match
,
route
=
this
.
routes
[
i
];
var
route
=
this
.
routes
[
i
],
match
;
if
(
match
=
target
.
match
(
route
.
route
))
{
var
location
=
route
.
target
.
split
(
'
:
'
),
host
=
location
[
0
],
...
...
lib/node-http-proxy/routing-proxy.js
0 → 100644
View file @
f7010e51
/*
* routing-proxy.js: A routing proxy consuming a RoutingTable and multiple HttpProxy instances
*
* (C) 2011 Nodejitsu Inc.
* MIT LICENCE
*
*/
var
events
=
require
(
'
events
'
),
util
=
require
(
'
util
'
),
HttpProxy
=
require
(
'
./http-proxy
'
).
HttpProxy
,
ProxyTable
=
require
(
'
./proxy-table
'
).
ProxyTable
;
//
// ### function RoutingProxy (options)
// #### @options {Object} Options for this instance
// Constructor function for the RoutingProxy object, a higher level
// reverse proxy Object which can proxy to multiple hosts and also interface
// easily with a RoutingTable instance.
//
var
RoutingProxy
=
exports
.
RoutingProxy
=
function
(
options
)
{
events
.
EventEmitter
.
call
(
this
);
var
self
=
this
;
options
=
options
||
{};
if
(
options
.
router
)
{
this
.
proxyTable
=
new
ProxyTable
(
options
);
this
.
proxyTable
.
on
(
'
routes
'
,
function
(
routes
)
{
self
.
emit
(
'
routes
'
,
routes
);
});
}
//
// Create a set of `HttpProxy` objects to be used later on calls
// to `.proxyRequest()` and `.proxyWebSocketRequest()`.
//
this
.
proxies
=
{};
//
// Setup default target options (such as `https`).
//
this
.
target
=
{};
this
.
target
.
https
=
options
.
target
&&
options
.
target
.
https
;
//
// Setup other default options to be used for instances of
// `HttpProxy` created by this `RoutingProxy` instance.
//
this
.
source
=
options
.
source
||
{
host
:
'
localhost
'
,
port
:
8000
};
this
.
https
=
this
.
source
.
https
||
options
.
https
;
this
.
enable
=
options
.
enable
;
this
.
forward
=
options
.
forward
;
};
//
// Inherit from `events.EventEmitter`.
//
util
.
inherits
(
RoutingProxy
,
events
.
EventEmitter
);
//
// ### function add (options)
// #### @options {Object} Options for the `HttpProxy` to add.
// Adds a new instance of `HttpProxy` to this `RoutingProxy` instance
// for the specified `options.host` and `options.port`.
//
RoutingProxy
.
prototype
.
add
=
function
(
options
)
{
var
self
=
this
,
key
=
this
.
_getKey
(
options
);
//
// TODO: Consume properties in `options` related to the `ProxyTable`.
//
options
.
target
=
options
.
target
||
{};
options
.
target
.
host
=
options
.
target
.
host
||
options
.
host
;
options
.
target
.
port
=
options
.
target
.
port
||
options
.
port
;
options
.
target
.
https
=
this
.
target
&&
this
.
target
.
https
||
options
.
target
&&
options
.
target
.
https
||
options
.
https
;
//
// Setup options to pass-thru to the new `HttpProxy` instance
// for the specified `options.host` and `options.port` pair.
//
[
'
https
'
,
'
enable
'
,
'
forward
'
].
forEach
(
function
(
key
)
{
if
(
options
[
key
]
!==
false
&&
self
[
key
])
{
options
[
key
]
=
self
[
key
];
}
});
this
.
proxies
[
key
]
=
new
HttpProxy
(
options
);
};
//
// ### function remove (options)
// #### @options {Object} Options mapping to the `HttpProxy` to remove.
// Removes an instance of `HttpProxy` from this `RoutingProxy` instance
// for the specified `options.host` and `options.port` (if they exist).
//
RoutingProxy
.
prototype
.
remove
=
function
(
options
)
{
var
key
=
this
.
_getKey
(
options
);
};
//
// ### function close()
// Cleans up any state left behind (sockets, timeouts, etc)
// associated with this instance.
//
RoutingProxy
.
prototype
.
close
=
function
()
{
var
self
=
this
;
if
(
this
.
proxyTable
)
{
//
// Close the `RoutingTable` associated with
// this instance (if any).
//
this
.
proxyTable
.
close
();
}
//
// Close all sockets for all `HttpProxy` object(s)
// associated with this instance.
//
Object
.
keys
(
this
.
proxies
).
forEach
(
function
(
key
)
{
self
.
proxies
[
key
].
close
();
});
};
//
// ### function proxyRequest (req, res, [port, host, paused])
// #### @req {ServerRequest} Incoming HTTP Request to proxy.
// #### @res {ServerResponse} Outgoing HTTP Request to write proxied data to.
// #### @options {Object} Options for the outgoing proxy request.
//
// options.port {number} Port to use on the proxy target host.
// options.host {string} Host of the proxy target.
// options.buffer {Object} Result from `httpProxy.buffer(req)`
// options.https {Object|boolean} Settings for https.
//
RoutingProxy
.
prototype
.
proxyRequest
=
function
(
req
,
res
,
options
)
{
options
=
options
||
{};
//
// Check the proxy table for this instance to see if we need
// to get the proxy location for the request supplied. We will
// always ignore the proxyTable if an explicit `port` and `host`
// arguments are supplied to `proxyRequest`.
//
if
(
this
.
proxyTable
&&
!
options
.
host
)
{
location
=
this
.
proxyTable
.
getProxyLocation
(
req
);
//
// If no location is returned from the ProxyTable instance
// then respond with `404` since we do not have a valid proxy target.
//
if
(
!
location
)
{
try
{
res
.
writeHead
(
404
);
res
.
end
();
}
catch
(
er
)
{
console
.
error
(
"
res.writeHead/res.end error: %s
"
,
er
.
message
);
}
return
;
}
//
// When using the ProxyTable in conjunction with an HttpProxy instance
// only the following arguments are valid:
//
// * `proxy.proxyRequest(req, res, { host: 'localhost' })`: This will be skipped
// * `proxy.proxyRequest(req, res, { buffer: buffer })`: Buffer will get updated appropriately
// * `proxy.proxyRequest(req, res)`: Options will be assigned appropriately.
//
options
.
port
=
location
.
port
;
options
.
host
=
location
.
host
;
}
var
key
=
options
.
host
+
'
:
'
+
options
.
port
,
proxy
;
if
(
!
this
.
proxies
[
key
])
{
this
.
add
(
options
);
}
proxy
=
this
.
proxies
[
key
];
proxy
.
proxyRequest
(
req
,
res
,
options
.
buffer
);
};
//
// ### function proxyWebSocketRequest (req, socket, head, options)
// #### @req {ServerRequest} Websocket request to proxy.
// #### @socket {net.Socket} Socket for the underlying HTTP request
// #### @head {string} Headers for the Websocket request.
// #### @options {Object} Options to use when proxying this request.
//
// options.port {number} Port to use on the proxy target host.
// options.host {string} Host of the proxy target.
// options.buffer {Object} Result from `httpProxy.buffer(req)`
// options.https {Object|boolean} Settings for https.
//
RoutingProxy
.
prototype
.
proxyWebSocketRequest
=
function
(
req
,
socket
,
head
,
options
)
{
options
=
options
||
{};
if
(
this
.
proxyTable
&&
!
options
.
host
)
{
location
=
this
.
proxyTable
.
getProxyLocation
(
req
);
if
(
!
location
)
{
return
socket
.
destroy
();
}
options
.
port
=
location
.
port
;
options
.
host
=
location
.
host
;
}
var
key
=
options
.
host
+
'
:
'
+
options
.
port
,
proxy
;
if
(
!
this
.
proxies
[
key
])
{
this
.
add
(
options
);
}
proxy
=
this
.
proxies
[
key
];
proxy
.
proxyWebSocketRequest
(
req
,
socket
,
head
,
options
.
buffer
);
};
//
// ### @private function _getKey (options)
// #### @options {Object} Options to extract the key from
// Ensures that the appropriate options are present in the `options`
// provided and responds with a string key representing the `host`, `port`
// combination contained within.
//
RoutingProxy
.
prototype
.
_getKey
=
function
(
options
)
{
if
(
!
options
||
((
!
options
.
host
||
!
options
.
port
)
&&
(
!
options
.
target
||
!
options
.
target
.
host
||
!
options
.
target
.
port
)))
{
throw
new
Error
(
'
options.host and options.port or options.target are required.
'
);
return
;
}
return
[
options
.
host
||
options
.
target
.
host
,
options
.
port
||
options
.
target
.
port
].
join
(
'
:
'
);
}
\ No newline at end of file
package.json
View file @
f7010e51
{
"name"
:
"http-proxy"
,
"description"
:
"A full-featured http reverse proxy for node.js"
,
"version"
:
"0.
6.6
"
,
"version"
:
"0.
7.0
"
,
"author"
:
"Charlie Robbins <charlie.robbins@gmail.com>"
,
"contributors"
:
[
{
"name"
:
"Mikeal Rogers"
,
"email"
:
"mikeal.rogers@gmail.com"
},
{
"name"
:
"Marak Squires"
,
"email"
:
"marak.squires@gmail.com"
},
{
"name"
:
"Fedor Indutny"
,
"email"
:
"fedor.indutny@gmail.com"
}
{
"name"
:
"Fedor Indutny"
,
"email"
:
"fedor.indutny@gmail.com"
},
{
"name"
:
"Dominic Tarr"
,
"email"
:
"dominic@nodejitsu.com"
}
],
"repository"
:
{
"type"
:
"git"
,
...
...
@@ -25,6 +26,10 @@
},
"main"
:
"./lib/node-http-proxy"
,
"bin"
:
{
"node-http-proxy"
:
"./bin/node-http-proxy"
},
"scripts"
:
{
"test"
:
"vows test/*-test.js --spec && vows test/*-test.js --spec --https"
},
"scripts"
:
{
"test"
:
"npm run-script test-http && npm run-script test-https"
,
"test-http"
:
"vows --spec && vows --spec --target=secure"
,
"test-https"
:
"vows --spec --source=secure && vows --spec --source=secure --target=secure"
},
"engines"
:
{
"node"
:
"0.4.x || 0.5.x"
}
}
test/helpers.js
View file @
f7010e51
...
...
@@ -5,27 +5,16 @@
*
*/
var
fs
=
require
(
'
fs
'
),
var
assert
=
require
(
'
assert
'
),
fs
=
require
(
'
fs
'
),
http
=
require
(
'
http
'
),
https
=
require
(
'
https
'
),
path
=
require
(
'
path
'
),
vows
=
require
(
'
vows
'
),
assert
=
require
(
'
assert
'
),
argv
=
require
(
'
optimist
'
).
argv
,
request
=
require
(
'
request
'
),
websocket
=
require
(
'
./../vendor/websocket
'
),
httpProxy
=
require
(
'
./../lib/node-http-proxy
'
);
function
merge
(
target
)
{
var
objs
=
Array
.
prototype
.
slice
.
call
(
arguments
,
1
);
objs
.
forEach
(
function
(
o
)
{
Object
.
keys
(
o
).
forEach
(
function
(
attr
)
{
if
(
!
o
.
__lookupGetter__
(
attr
))
{
target
[
attr
]
=
o
[
attr
];
}
});
});
return
target
;
}
vows
=
require
(
'
vows
'
),
websocket
=
require
(
'
../vendor/websocket
'
),
httpProxy
=
require
(
'
../lib/node-http-proxy
'
);
var
loadHttps
=
exports
.
loadHttps
=
function
()
{
return
{
...
...
@@ -34,21 +23,35 @@ var loadHttps = exports.loadHttps = function () {
};
};
var
TestRunner
=
exports
.
TestRunner
=
function
(
protocol
,
target
)
{
this
.
options
=
{};
this
.
options
.
target
=
{};
this
.
protocol
=
protocol
;
this
.
target
=
target
;
var
parseProtocol
=
exports
.
parseProtocol
=
function
()
{
function
setupProtocol
(
secure
)
{
return
{
secure
:
secure
,
protocols
:
{
http
:
secure
?
'
https
'
:
'
http
'
,
ws
:
secure
?
'
wss
'
:
'
ws
'
}
}
}
return
{
source
:
setupProtocol
(
argv
.
source
===
'
secure
'
),
target
:
setupProtocol
(
argv
.
target
===
'
secure
'
)
};
}
var
TestRunner
=
exports
.
TestRunner
=
function
(
options
)
{
options
=
options
||
{};
this
.
source
=
options
.
source
||
{};
this
.
target
=
options
.
target
||
{};
this
.
testServers
=
[];
if
(
protocol
===
'
https
'
)
{
this
.
options
.
https
=
loadHttps
();
if
(
this
.
source
.
secure
)
{
this
.
source
.
https
=
loadHttps
();
}
if
(
target
===
'
https
'
)
{
this
.
options
.
target
=
{
https
:
loadHttps
()
};
if
(
this
.
target
.
secure
)
{
this
.
target
.
https
=
loadHttps
();
}
};
...
...
@@ -59,14 +62,18 @@ TestRunner.prototype.assertProxied = function (host, proxyPort, port, createProx
var
test
=
{
topic
:
function
()
{
var
that
=
this
,
options
=
{
var
that
=
this
,
options
;
options
=
{
method
:
'
GET
'
,
uri
:
self
.
protocol
+
'
://localhost:
'
+
proxyPort
,
uri
:
self
.
source
.
protocols
.
http
+
'
://localhost:
'
+
proxyPort
,
headers
:
{
host
:
host
}
};
function
startTest
()
{
if
(
port
)
{
return
self
.
startTargetServer
(
port
,
output
,
function
()
{
...
...
@@ -91,7 +98,7 @@ TestRunner.prototype.assertProxied = function (host, proxyPort, port, createProx
TestRunner
.
prototype
.
assertResponseCode
=
function
(
proxyPort
,
statusCode
,
createProxy
)
{
var
assertion
=
"
should receive
"
+
statusCode
+
"
responseCode
"
,
protocol
=
this
.
protocol
;
protocol
=
this
.
source
.
protocols
.
http
;
var
test
=
{
topic
:
function
()
{
...
...
@@ -124,7 +131,6 @@ TestRunner.prototype.assertResponseCode = function (proxyPort, statusCode, creat
//
// WebSocketTest
//
TestRunner
.
prototype
.
webSocketTest
=
function
(
options
)
{
var
self
=
this
;
...
...
@@ -161,7 +167,6 @@ TestRunner.prototype.webSocketTest = function (options) {
//
// WebSocketTestWithTable
//
TestRunner
.
prototype
.
webSocketTestWithTable
=
function
(
options
)
{
var
self
=
this
;
...
...
@@ -174,7 +179,7 @@ TestRunner.prototype.webSocketTestWithTable = function (options) {
self
.
startProxyServerWithTable
(
options
.
ports
.
proxy
,
{
router
:
options
.
router
},
{
router
:
options
.
router
},
function
(
err
,
proxy
)
{
if
(
options
.
onServer
)
{
options
.
onServer
(
proxy
)
}
...
...
@@ -199,8 +204,7 @@ TestRunner.prototype.webSocketTestWithTable = function (options) {
//
TestRunner
.
prototype
.
startProxyServer
=
function
(
port
,
targetPort
,
host
,
callback
)
{
var
that
=
this
,
options
=
that
.
options
,
proxyServer
=
httpProxy
.
createServer
(
targetPort
,
host
,
options
);
proxyServer
=
httpProxy
.
createServer
(
host
,
targetPort
,
this
.
getOptions
());
proxyServer
.
listen
(
port
,
function
()
{
that
.
testServers
.
push
(
proxyServer
);
...
...
@@ -212,18 +216,19 @@ TestRunner.prototype.startProxyServer = function (port, targetPort, host, callba
// Creates the reverse proxy server with a specified latency
//
TestRunner
.
prototype
.
startLatentProxyServer
=
function
(
port
,
targetPort
,
host
,
latency
,
callback
)
{
//
// Initialize the nodeProxy and start proxying the request
var
that
=
this
,
proxyServer
=
httpProxy
.
createServer
(
function
(
req
,
res
,
proxy
)
{
var
buffer
=
proxy
.
buffer
(
req
);
//
var
that
=
this
,
proxyServer
;
proxyServer
=
httpProxy
.
createServer
(
host
,
targetPort
,
function
(
req
,
res
,
proxy
)
{
var
buffer
=
httpProxy
.
buffer
(
req
);
setTimeout
(
function
()
{
proxy
.
proxyRequest
(
req
,
res
,
{
port
:
targetPort
,
host
:
host
,
buffer
:
buffer
});
proxy
.
proxyRequest
(
req
,
res
,
buffer
);
},
latency
);
},
this
.
options
);
},
this
.
getOptions
()
);
proxyServer
.
listen
(
port
,
function
()
{
that
.
testServers
.
push
(
proxyServer
);
...
...
@@ -235,7 +240,9 @@ TestRunner.prototype.startLatentProxyServer = function (port, targetPort, host,
// Creates the reverse proxy server with a ProxyTable
//
TestRunner
.
prototype
.
startProxyServerWithTable
=
function
(
port
,
options
,
callback
)
{
var
that
=
this
,
proxyServer
=
httpProxy
.
createServer
(
merge
({},
options
,
this
.
options
));
var
that
=
this
,
proxyServer
=
httpProxy
.
createServer
(
merge
({},
options
,
this
.
getOptions
()));
proxyServer
.
listen
(
port
,
function
()
{
that
.
testServers
.
push
(
proxyServer
);
callback
();
...
...
@@ -248,13 +255,15 @@ TestRunner.prototype.startProxyServerWithTable = function (port, options, callba
// Creates a latent reverse proxy server using a ProxyTable
//
TestRunner
.
prototype
.
startProxyServerWithTableAndLatency
=
function
(
port
,
latency
,
options
,
callback
)
{
//
// Initialize the nodeProxy and start proxying the request
var
proxyServer
,
that
=
this
,
proxy
=
new
httpProxy
.
HttpProxy
(
merge
({},
options
,
that
.
options
));
//
var
that
=
this
,
proxy
=
new
httpProxy
.
RoutingProxy
(
merge
({},
options
,
this
.
getOptions
())),
proxyServer
;
var
handler
=
function
(
req
,
res
)
{
var
buffer
=
p
roxy
.
buffer
(
req
);
var
buffer
=
httpP
roxy
.
buffer
(
req
);
setTimeout
(
function
()
{
proxy
.
proxyRequest
(
req
,
res
,
{
buffer
:
buffer
...
...
@@ -262,9 +271,9 @@ TestRunner.prototype.startProxyServerWithTableAndLatency = function (port, laten
},
latency
);
};
proxyServer
=
th
at
.
options
.
https
?
https
.
createServer
(
that
.
options
.
https
,
handler
,
that
.
options
)
:
http
.
createServer
(
handler
,
that
.
options
);
proxyServer
=
th
is
.
source
.
https
?
https
.
createServer
(
this
.
source
.
https
,
handler
)
:
http
.
createServer
(
handler
);
proxyServer
.
listen
(
port
,
function
()
{
that
.
testServers
.
push
(
proxyServer
);
...
...
@@ -278,7 +287,9 @@ TestRunner.prototype.startProxyServerWithTableAndLatency = function (port, laten
// Creates proxy server forwarding to the specified options
//
TestRunner
.
prototype
.
startProxyServerWithForwarding
=
function
(
port
,
targetPort
,
host
,
options
,
callback
)
{
var
that
=
this
,
proxyServer
=
httpProxy
.
createServer
(
targetPort
,
host
,
merge
({},
options
,
this
.
options
));
var
that
=
this
,
proxyServer
=
httpProxy
.
createServer
(
targetPort
,
host
,
merge
({},
options
,
this
.
getOptions
()));
proxyServer
.
listen
(
port
,
function
()
{
that
.
testServers
.
push
(
proxyServer
);
callback
(
null
,
proxyServer
);
...
...
@@ -289,14 +300,18 @@ TestRunner.prototype.startProxyServerWithForwarding = function (port, targetPort
// Creates the 'hellonode' server
//
TestRunner
.
prototype
.
startTargetServer
=
function
(
port
,
output
,
callback
)
{
var
that
=
this
,
targetServer
,
handler
=
function
(
req
,
res
)
{
var
that
=
this
,
targetServer
,
handler
;
handler
=
function
(
req
,
res
)
{
res
.
writeHead
(
200
,
{
'
Content-Type
'
:
'
text/plain
'
});
res
.
write
(
output
);
res
.
end
();
};
targetServer
=
this
.
options
.
target
.
https
?
https
.
createServer
(
this
.
options
.
target
.
https
,
handler
)
targetServer
=
this
.
target
.
https
?
https
.
createServer
(
this
.
target
.
https
,
handler
)
:
http
.
createServer
(
handler
);
targetServer
.
listen
(
port
,
function
()
{
...
...
@@ -315,3 +330,42 @@ TestRunner.prototype.closeServers = function () {
return
this
.
testServers
;
};
//
// Creates a new instance of the options to
// pass to `httpProxy.createServer()`
//
TestRunner
.
prototype
.
getOptions
=
function
()
{
return
{
https
:
clone
(
this
.
source
.
https
),
target
:
{
https
:
clone
(
this
.
target
.
https
)
}
};
};
//
// ### @private function clone (object)
// #### @object {Object} Object to clone
// Shallow clones the specified object.
//
function
clone
(
object
)
{
if
(
!
object
)
{
return
null
}
return
Object
.
keys
(
object
).
reduce
(
function
(
obj
,
k
)
{
obj
[
k
]
=
object
[
k
];
return
obj
;
},
{});
}
function
merge
(
target
)
{
var
objs
=
Array
.
prototype
.
slice
.
call
(
arguments
,
1
);
objs
.
forEach
(
function
(
o
)
{
Object
.
keys
(
o
).
forEach
(
function
(
attr
)
{
if
(
!
o
.
__lookupGetter__
(
attr
))
{
target
[
attr
]
=
o
[
attr
];
}
});
});
return
target
;
}
\ No newline at end of file
test/
node-
http-proxy-test.js
→
test/
http/
http-proxy-test.js
View file @
f7010e51
...
...
@@ -26,10 +26,9 @@
var
assert
=
require
(
'
assert
'
),
util
=
require
(
'
util
'
),
argv
=
require
(
'
optimist
'
).
argv
,
request
=
require
(
'
request
'
),
vows
=
require
(
'
vows
'
),
helpers
=
require
(
'
./helpers
'
);
helpers
=
require
(
'
.
.
/helpers
'
);
var
forwardOptions
=
{
forward
:
{
...
...
@@ -45,11 +44,11 @@ var badForwardOptions = {
}
};
var
protocol
=
argv
.
https
?
'
https
'
:
'
http
'
,
t
arget
=
argv
.
target
?
argv
.
target
:
'
http
'
,
runner
=
new
helpers
.
TestRunner
(
protocol
,
target
);
var
options
=
helpers
.
parseProtocol
()
,
t
estName
=
[
options
.
source
.
protocols
.
http
,
options
.
target
.
protocols
.
http
].
join
(
'
-to-
'
)
,
runner
=
new
helpers
.
TestRunner
(
options
);
vows
.
describe
(
'
node-http-proxy/
'
+
protocol
).
addBatch
({
vows
.
describe
(
'
node-http-proxy/
http-proxy/
'
+
testName
).
addBatch
({
"
When using server created by httpProxy.createServer()
"
:
{
"
with no latency
"
:
{
"
and a valid target server
"
:
runner
.
assertProxied
(
'
localhost
'
,
8080
,
8081
,
function
(
callback
)
{
...
...
test/
proxy-table
-test.js
→
test/
http/routing-proxy
-test.js
View file @
f7010e51
...
...
@@ -12,11 +12,11 @@ var assert = require('assert'),
argv
=
require
(
'
optimist
'
).
argv
,
request
=
require
(
'
request
'
),
vows
=
require
(
'
vows
'
),
helpers
=
require
(
'
./helpers
'
),
TestRunner
=
helpers
.
TestRunner
;
helpers
=
require
(
'
../helpers
'
);
var
protocol
=
argv
.
https
?
'
https
'
:
'
http
'
,
runner
=
new
TestRunner
(
protocol
),
var
options
=
helpers
.
parseProtocol
(),
testName
=
[
options
.
source
.
protocols
.
http
,
options
.
target
.
protocols
.
http
].
join
(
'
-to-
'
),
runner
=
new
helpers
.
TestRunner
(
options
),
routeFile
=
path
.
join
(
__dirname
,
'
config.json
'
);
var
fileOptions
=
{
...
...
@@ -41,7 +41,7 @@ var hostnameOptions = {
}
};
vows
.
describe
(
'
node-http-proxy/
proxy-table/
'
+
protocol
).
addBatch
({
vows
.
describe
(
'
node-http-proxy/
routing-proxy/
'
+
testName
).
addBatch
({
"
When using server created by httpProxy.createServer()
"
:
{
"
when passed a routing table
"
:
{
"
and routing by RegExp
"
:
{
...
...
@@ -82,16 +82,14 @@ vows.describe('node-http-proxy/proxy-table/' + protocol).addBatch({
fs
.
writeFileSync
(
routeFile
,
JSON
.
stringify
(
config
));
this
.
server
.
on
(
'
routes
'
,
function
()
{
var
options
=
{
runner
.
startTargetServer
(
8103
,
'
hello dynamic.com
'
,
function
()
{
request
({
method
:
'
GET
'
,
uri
:
protocol
+
'
://localhost:8100
'
,
uri
:
options
.
source
.
protocols
.
http
+
'
://localhost:8100
'
,
headers
:
{
host
:
'
dynamic.com
'
}
};
runner
.
startTargetServer
(
8103
,
'
hello dynamic.com
'
,
function
()
{
request
(
options
,
that
.
callback
);
},
that
.
callback
);
});
});
},
...
...
test/web
-
socket-proxy-test.js
→
test/web
socket/web
socket-proxy-test.js
View file @
f7010e51
...
...
@@ -30,8 +30,8 @@ var util = require('util'),
colors
=
require
(
'
colors
'
),
request
=
require
(
'
request
'
),
vows
=
require
(
'
vows
'
),
websocket
=
require
(
'
./../vendor/websocket
'
),
helpers
=
require
(
'
./helpers
'
);
websocket
=
require
(
'
.
.
/../vendor/websocket
'
),
helpers
=
require
(
'
.
.
/helpers
'
);
try
{
var
utils
=
require
(
'
socket.io/lib/socket.io/utils
'
),
...
...
@@ -43,55 +43,11 @@ catch (ex) {
process
.
exit
(
1
);
}
var
protocol
=
argv
.
https
?
'
https
'
:
'
http
'
,
wsprotocol
=
argv
.
https
?
'
wss
'
:
'
ws
'
,
runner
=
new
helpers
.
TestRunner
(
protocol
);
var
options
=
helpers
.
parseProtocol
()
,
testName
=
[
options
.
source
.
protocols
.
ws
,
options
.
target
.
protocols
.
ws
].
join
(
'
-to-
'
)
,
runner
=
new
helpers
.
TestRunner
(
options
);
vows
.
describe
(
'
node-http-proxy/websocket/
'
+
wsprotocol
).
addBatch
({
"
when using proxy table
"
:{
"
with no latency
"
:
{
"
when an inbound message is sent from a WebSocket client
"
:
{
topic
:
function
()
{
var
that
=
this
headers
=
{};
runner
.
webSocketTestWithTable
({
io
:
io
,
host
:
'
localhost
'
,
wsprotocol
:
wsprotocol
,
protocol
:
protocol
,
router
:
{
'
localhost
'
:
'
localhost:8230
'
},
ports
:
{
target
:
8230
,
proxy
:
8231
},
onListen
:
function
(
socket
)
{
socket
.
on
(
'
connection
'
,
function
(
client
)
{
client
.
on
(
'
message
'
,
function
(
msg
)
{
that
.
callback
(
null
,
msg
,
headers
);
});
});
},
onWsupgrade
:
function
(
req
,
res
)
{
headers
.
request
=
req
;
headers
.
response
=
res
.
headers
;
},
onOpen
:
function
(
ws
)
{
ws
.
send
(
utils
.
encode
(
'
from client
'
));
}
});
},
"
the target server should receive the message
"
:
function
(
err
,
msg
,
headers
)
{
assert
.
equal
(
msg
,
'
from client
'
);
},
"
the origin and sec-websocket-origin headers should match
"
:
function
(
err
,
msg
,
headers
)
{
assert
.
isString
(
headers
.
response
[
'
sec-websocket-location
'
]);
assert
.
isTrue
(
headers
.
response
[
'
sec-websocket-location
'
].
indexOf
(
wsprotocol
)
!==
-
1
);
assert
.
equal
(
headers
.
request
.
Origin
,
headers
.
response
[
'
sec-websocket-origin
'
]);
}
}
}
},
vows
.
describe
(
'
node-http-proxy/http-proxy/
'
+
testName
).
addBatch
({
"
When using server created by httpProxy.createServer()
"
:
{
"
with no latency
"
:
{
"
when an inbound message is sent from a WebSocket client
"
:
{
...
...
@@ -102,8 +58,8 @@ vows.describe('node-http-proxy/websocket/' + wsprotocol).addBatch({
runner
.
webSocketTest
({
io
:
io
,
host
:
'
localhost
'
,
wsprotocol
:
wsprotocol
,
protocol
:
protocol
,
wsprotocol
:
options
.
source
.
protocols
.
ws
,
protocol
:
options
.
source
.
protocols
.
http
,
ports
:
{
target
:
8130
,
proxy
:
8131
...
...
@@ -129,7 +85,7 @@ vows.describe('node-http-proxy/websocket/' + wsprotocol).addBatch({
},
"
the origin and sec-websocket-origin headers should match
"
:
function
(
err
,
msg
,
headers
)
{
assert
.
isString
(
headers
.
response
[
'
sec-websocket-location
'
]);
assert
.
isTrue
(
headers
.
response
[
'
sec-websocket-location
'
].
indexOf
(
wsprotocol
)
!==
-
1
);
assert
.
isTrue
(
headers
.
response
[
'
sec-websocket-location
'
].
indexOf
(
options
.
source
.
protocols
.
ws
)
!==
-
1
);
assert
.
equal
(
headers
.
request
.
Origin
,
headers
.
response
[
'
sec-websocket-origin
'
]);
}
},
...
...
@@ -141,8 +97,8 @@ vows.describe('node-http-proxy/websocket/' + wsprotocol).addBatch({
runner
.
webSocketTest
({
io
:
io
,
host
:
'
localhost
'
,
wsprotocol
:
wsprotocol
,
protocol
:
protocol
,
wsprotocol
:
options
.
source
.
protocols
.
ws
,
protocol
:
options
.
source
.
protocols
.
http
,
ports
:
{
target
:
8132
,
proxy
:
8133
...
...
@@ -170,8 +126,8 @@ vows.describe('node-http-proxy/websocket/' + wsprotocol).addBatch({
runner
.
webSocketTest
({
io
:
io
,
host
:
'
localhost
'
,
wsprotocol
:
wsprotocol
,
protocol
:
protocol
,
wsprotocol
:
options
.
source
.
protocols
.
ws
,
protocol
:
options
.
source
.
protocols
.
http
,
ports
:
{
target
:
8134
,
proxy
:
8135
...
...
@@ -198,7 +154,7 @@ vows.describe('node-http-proxy/websocket/' + wsprotocol).addBatch({
},
"
the origin and sec-websocket-origin headers should match
"
:
function
(
err
,
msg
,
headers
)
{
assert
.
isString
(
headers
.
response
[
'
sec-websocket-location
'
]);
assert
.
isTrue
(
headers
.
response
[
'
sec-websocket-location
'
].
indexOf
(
wsprotocol
)
!==
-
1
);
assert
.
isTrue
(
headers
.
response
[
'
sec-websocket-location
'
].
indexOf
(
options
.
source
.
protocols
.
ws
)
!==
-
1
);
assert
.
equal
(
headers
.
request
.
Origin
,
headers
.
response
[
'
sec-websocket-origin
'
]);
}
}
...
...
test/websocket/websocket-routing-proxy-test.js
0 → 100644
View file @
f7010e51
/*
node-http-proxy-test.js: http proxy for node.js
Copyright (c) 2010 Charlie Robbins, Marak Squires and Fedor Indutny
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
util
=
require
(
'
util
'
),
assert
=
require
(
'
assert
'
),
argv
=
require
(
'
optimist
'
).
argv
,
colors
=
require
(
'
colors
'
),
request
=
require
(
'
request
'
),
vows
=
require
(
'
vows
'
),
websocket
=
require
(
'
../../vendor/websocket
'
),
helpers
=
require
(
'
../helpers
'
);
try
{
var
utils
=
require
(
'
socket.io/lib/socket.io/utils
'
),
io
=
require
(
'
socket.io
'
);
}
catch
(
ex
)
{
console
.
error
(
'
Socket.io is required for this example:
'
);
console
.
error
(
'
npm
'
+
'
install
'
.
green
+
'
socket.io@0.6.18
'
.
magenta
);
process
.
exit
(
1
);
}
var
options
=
helpers
.
parseProtocol
(),
testName
=
[
options
.
source
.
protocols
.
ws
,
options
.
target
.
protocols
.
ws
].
join
(
'
-to-
'
),
runner
=
new
helpers
.
TestRunner
(
options
);
vows
.
describe
(
'
node-http-proxy/routing-proxy/
'
+
testName
).
addBatch
({
"
When using server created by httpProxy.createServer()
"
:
{
"
using proxy table with no latency
"
:
{
"
when an inbound message is sent from a WebSocket client
"
:
{
topic
:
function
()
{
var
that
=
this
headers
=
{};
runner
.
webSocketTestWithTable
({
io
:
io
,
host
:
'
localhost
'
,
wsprotocol
:
options
.
source
.
protocols
.
ws
,
protocol
:
options
.
source
.
protocols
.
http
,
router
:
{
'
localhost
'
:
'
localhost:8230
'
},
ports
:
{
target
:
8230
,
proxy
:
8231
},
onListen
:
function
(
socket
)
{
socket
.
on
(
'
connection
'
,
function
(
client
)
{
client
.
on
(
'
message
'
,
function
(
msg
)
{
that
.
callback
(
null
,
msg
,
headers
);
});
});
},
onWsupgrade
:
function
(
req
,
res
)
{
headers
.
request
=
req
;
headers
.
response
=
res
.
headers
;
},
onOpen
:
function
(
ws
)
{
ws
.
send
(
utils
.
encode
(
'
from client
'
));
}
});
},
"
the target server should receive the message
"
:
function
(
err
,
msg
,
headers
)
{
assert
.
equal
(
msg
,
'
from client
'
);
},
"
the origin and sec-websocket-origin headers should match
"
:
function
(
err
,
msg
,
headers
)
{
assert
.
isString
(
headers
.
response
[
'
sec-websocket-location
'
]);
assert
.
isTrue
(
headers
.
response
[
'
sec-websocket-location
'
].
indexOf
(
options
.
source
.
protocols
.
ws
)
!==
-
1
);
assert
.
equal
(
headers
.
request
.
Origin
,
headers
.
response
[
'
sec-websocket-origin
'
]);
}
}
}
}
}).
addBatch
({
"
When the tests are over
"
:
{
topic
:
function
()
{
return
runner
.
closeServers
();
},
"
the servers should clean up
"
:
function
()
{
assert
.
isTrue
(
true
);
}
}
}).
export
(
module
);
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