Skip to content
Browse files

Merge branch 'master' into experimental

  • Loading branch information...
2 parents d3486a6 + 0911c17 commit 8f28618b80d328f17a60c3d8c9ad64b6bebfd496 @indexzero indexzero committed
View
94 README.md
@@ -216,24 +216,56 @@ proxyServerWithForwarding.listen(80);
The forwarding option can be used in conjunction with the proxy table options by simply including both the 'forward' and 'router' properties in the options passed to 'createServer'.
-### 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:
+## Using HTTPS
+You have all the full flexibility of node-http-proxy offers in HTTPS as well as HTTP. The two basic scenarios are: with a stand-alone proxy server or in conjunction with another HTTPS server.
+
+### Proxying to HTTP from HTTPS
+This is probably the most common use-case for proxying in conjunction with HTTPS. You have some front-facing HTTPS server, but all of your internal traffic is HTTP. In this way, you can reduce the number of servers to which your CA and other important security files are deployed and reduce the computational overhead from HTTPS traffic.
+
+Using HTTPS in `node-http-proxy` is relatively straight-forward:
+
``` js
-usage: node-http-proxy [options]
+var fs = require('fs'),
+ http = require('http'),
+ https = require('https'),
+ httpProxy = require('http-proxy');
+
+var options = {
+ https: {
+ key: fs.readFileSync('path/to/your/key.pem', 'utf8'),
+ cert: fs.readFileSync('path/to/your/cert.pem', 'utf8')
+ }
+};
-All options should be set with the syntax --option=value
+//
+// Create a standalone HTTPS proxy server
+//
+httpProxy.createServer(8000, 'localhost', options).listen(8001);
-options:
- --port PORT Port that the proxy server should run on
- --target HOST:PORT Location of the server the proxy will target
- --config OUTFILE Location of the configuration file for the proxy server
- --silent Silence the log output from the proxy server
- -h, --help You're staring at it
+//
+// Create an instance of HttpProxy to use with another HTTPS server
+//
+var proxy = new httpProxy.HttpProxy();
+https.createServer(options.https, function (req, res) {
+ proxy.proxyRequest(req, res, {
+ host: 'localhost',
+ port: 8000
+ })
+}).listen(8002);
+
+//
+// Create the target HTTPS server for both cases
+//
+http.createServer(function (req, res) {
+ res.writeHead(200, { 'Content-Type': 'text/plain' });
+ res.write('hello https\n');
+ res.end();
+}).listen(8000);
```
-### Proxying over HTTPS
-You have all the full flexibility of node-http-proxy offers in HTTPS as well as HTTP. The two basic scenarios are: with a stand-alone proxy server or in conjunction with another HTTPS server.
+### Proxying to HTTPS from HTTPS
+Proxying from HTTPS to HTTPS is essentially the same as proxying from HTTPS to HTTP, but you must include `target` option in when calling `httpProxy.createServer` or instantiating a new instance of `HttpProxy`.
``` js
var fs = require('fs'),
@@ -244,6 +276,9 @@ var options = {
https: {
key: fs.readFileSync('path/to/your/key.pem', 'utf8'),
cert: fs.readFileSync('path/to/your/cert.pem', 'utf8')
+ },
+ target: {
+ https: true // This could also be an Object with key and cert properties
}
};
@@ -255,7 +290,12 @@ httpProxy.createServer(8000, 'localhost', options).listen(8001);
//
// Create an instance of HttpProxy to use with another HTTPS server
//
-var proxy = new httpProxy.HttpProxy({ https: true });
+var proxy = new httpProxy.HttpProxy({
+ target: {
+ https: true
+ }
+});
+
https.createServer(options.https, function (req, res) {
proxy.proxyRequest(req, res, {
host: 'localhost',
@@ -273,7 +313,7 @@ https.createServer(options.https, function (req, res) {
}).listen(8000);
```
-### Proxying WebSockets
+## Proxying WebSockets
Websockets are handled automatically when using the `httpProxy.createServer()`, but if you want to use it in conjunction with a stand-alone HTTP + WebSocket (such as [socket.io][5]) server here's how:
``` js
@@ -306,16 +346,40 @@ server.on('upgrade', function(req, socket, head) {
});
```
+## 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:
+
+``` js
+usage: node-http-proxy [options]
+
+All options should be set with the syntax --option=value
+
+options:
+ --port PORT Port that the proxy server should run on
+ --target HOST:PORT Location of the server the proxy will target
+ --config OUTFILE Location of the configuration file for the proxy server
+ --silent Silence the log output from the proxy server
+ -h, --help You're staring at it
+```
+
<br/>
-### Why doesn't node-http-proxy have more advanced features like x, y, or z?
+## Why doesn't node-http-proxy have more advanced features like x, y, or z?
If you have a suggestion for a feature currently not supported, feel free to open a [support issue][6]. node-http-proxy is designed to just proxy http requests from one server to another, but we will be soon releasing many other complimentary projects that can be used in conjunction with node-http-proxy.
## Run Tests
+The test suite is designed to fully cover the combinatoric possibilities of HTTP and HTTPS proxying:
+
+1. HTTP --> HTTP
+2. HTTPS --> HTTP
+3. HTTPS --> HTTPS
+4. HTTP --> HTTPS
```
vows test/*-test.js --spec
vows test/*-test.js --spec --https
+vows test/*-test.js --spec --https --target=https
+vows test/*-test.js --spec --target=https
```
<br/>
View
90 docs/balancing-proxy.html
@@ -0,0 +1,90 @@
+<!DOCTYPE html> <html> <head> <title>balancing-proxy.js</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <link rel="stylesheet" media="all" href="docco.css" /> </head> <body> <div id="container"> <div id="background"></div> <div id="jump_to"> Jump To &hellip; <div id="jump_wrapper"> <div id="jump_page"> <a class="source" href="balancing-proxy.html"> balancing-proxy.html </a> <a class="source" href="node-http-proxy.html"> node-http-proxy.html </a> <a class="source" href="proxy-table.html"> proxy-table.html </a> </div> </div> </div> <table cellpadding="0" cellspacing="0"> <thead> <tr> <th class="docs"> <h1> balancing-proxy.js </h1> </th> <th class="code"> </th> </tr> </thead> <tbody> <tr id="section-1"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-1">&#182;</a> </div> </td> <td class="code"> <div class="highlight"><pre><span class="cm">/*</span>
+<span class="cm"> balancing-proxy.js: Transparent Load-Balancing Optimized HTTP Proxy </span>
+
+<span class="cm"> Copyright (c) 2011 Charlie Robbins </span>
+
+<span class="cm"> Permission is hereby granted, free of charge, to any person obtaining</span>
+<span class="cm"> a copy of this software and associated documentation files (the</span>
+<span class="cm"> &quot;Software&quot;), to deal in the Software without restriction, including</span>
+<span class="cm"> without limitation the rights to use, copy, modify, merge, publish,</span>
+<span class="cm"> distribute, sublicense, and/or sell copies of the Software, and to</span>
+<span class="cm"> permit persons to whom the Software is furnished to do so, subject to</span>
+<span class="cm"> the following conditions:</span>
+
+<span class="cm"> The above copyright notice and this permission notice shall be</span>
+<span class="cm"> included in all copies or substantial portions of the Software.</span>
+
+<span class="cm"> THE SOFTWARE IS PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY OF ANY KIND,</span>
+<span class="cm"> EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF</span>
+<span class="cm"> MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND</span>
+<span class="cm"> NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE</span>
+<span class="cm"> LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION</span>
+<span class="cm"> OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION</span>
+<span class="cm"> WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.</span>
+
+<span class="cm">*/</span>
+
+<span class="kd">var</span> <span class="nx">net</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;net&#39;</span><span class="p">),</span>
+ <span class="nx">HTTPParser</span> <span class="o">=</span> <span class="nx">process</span><span class="p">.</span><span class="nx">binding</span><span class="p">(</span><span class="s1">&#39;http_parser&#39;</span><span class="p">).</span><span class="nx">HTTPParser</span><span class="p">,</span>
+ <span class="nx">streams</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;morestreams&#39;</span><span class="p">);</span>
+
+<span class="nx">exports</span><span class="p">.</span><span class="nx">createServer</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>
+ <span class="kd">var</span> <span class="nx">args</span> <span class="o">=</span> <span class="nb">Array</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">slice</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">arguments</span><span class="p">),</span>
+ <span class="nx">callback</span> <span class="o">=</span> <span class="k">typeof</span> <span class="nx">args</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">===</span> <span class="s1">&#39;function&#39;</span> <span class="o">&amp;&amp;</span> <span class="nx">args</span><span class="p">.</span><span class="nx">shift</span><span class="p">(),</span>
+ <span class="nx">options</span> <span class="o">=</span> <span class="p">{},</span> <span class="nx">port</span><span class="p">,</span> <span class="nx">host</span><span class="p">,</span> <span class="nx">server</span><span class="p">;</span>
+
+ <span class="nx">server</span> <span class="o">=</span> <span class="nx">net</span><span class="p">.</span><span class="nx">createServer</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">socket</span><span class="p">)</span> <span class="p">{</span>
+ <span class="kd">var</span> <span class="nx">buffer</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">streams</span><span class="p">.</span><span class="nx">BufferedStream</span><span class="p">(),</span>
+ <span class="nx">parser</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">HTTPParser</span><span class="p">(</span><span class="s1">&#39;request&#39;</span><span class="p">);</span>
+
+ <span class="nx">parser</span><span class="p">.</span><span class="nx">onHeaderField</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">b</span><span class="p">,</span> <span class="nx">start</span><span class="p">,</span> <span class="nx">len</span><span class="p">)</span> <span class="p">{</span>
+ <span class="kd">var</span> <span class="nx">slice</span> <span class="o">=</span> <span class="nx">b</span><span class="p">.</span><span class="nx">toString</span><span class="p">(</span><span class="s1">&#39;ascii&#39;</span><span class="p">,</span> <span class="nx">start</span><span class="p">,</span> <span class="nx">start</span> <span class="o">+</span> <span class="nx">len</span><span class="p">).</span><span class="nx">toLowerCase</span><span class="p">();</span>
+ <span class="k">if</span> <span class="p">(</span><span class="nx">parser</span><span class="p">.</span><span class="nx">value</span> <span class="o">!=</span> <span class="kc">undefined</span><span class="p">)</span> <span class="p">{</span>
+ <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;eyes&#39;</span><span class="p">).</span><span class="nx">inspect</span><span class="p">(</span><span class="nx">parser</span><span class="p">.</span><span class="nx">value</span><span class="p">,</span> <span class="nx">parser</span><span class="p">.</span><span class="nx">field</span><span class="p">);</span>
+ <span class="nx">parser</span><span class="p">.</span><span class="nx">field</span> <span class="o">=</span> <span class="kc">null</span><span class="p">;</span>
+ <span class="nx">parser</span><span class="p">.</span><span class="nx">value</span> <span class="o">=</span> <span class="kc">null</span><span class="p">;</span>
+ <span class="p">}</span>
+ <span class="k">if</span> <span class="p">(</span><span class="nx">parser</span><span class="p">.</span><span class="nx">field</span><span class="p">)</span> <span class="p">{</span>
+ <span class="nx">parser</span><span class="p">.</span><span class="nx">field</span> <span class="o">+=</span> <span class="nx">slice</span><span class="p">;</span>
+ <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
+ <span class="nx">parser</span><span class="p">.</span><span class="nx">field</span> <span class="o">=</span> <span class="nx">slice</span><span class="p">;</span>
+ <span class="p">}</span>
+ <span class="p">};</span>
+
+ <span class="nx">parser</span><span class="p">.</span><span class="nx">onHeaderValue</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">b</span><span class="p">,</span> <span class="nx">start</span><span class="p">,</span> <span class="nx">len</span><span class="p">)</span> <span class="p">{</span>
+ <span class="kd">var</span> <span class="nx">slice</span> <span class="o">=</span> <span class="nx">b</span><span class="p">.</span><span class="nx">toString</span><span class="p">(</span><span class="s1">&#39;ascii&#39;</span><span class="p">,</span> <span class="nx">start</span><span class="p">,</span> <span class="nx">start</span> <span class="o">+</span> <span class="nx">len</span><span class="p">);</span>
+ <span class="k">if</span> <span class="p">(</span><span class="nx">parser</span><span class="p">.</span><span class="nx">value</span><span class="p">)</span> <span class="p">{</span>
+ <span class="nx">parser</span><span class="p">.</span><span class="nx">value</span> <span class="o">+=</span> <span class="nx">slice</span><span class="p">;</span>
+ <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
+ <span class="nx">parser</span><span class="p">.</span><span class="nx">value</span> <span class="o">=</span> <span class="nx">slice</span><span class="p">;</span>
+ <span class="p">}</span>
+ <span class="p">};</span>
+
+ <span class="nx">parser</span><span class="p">.</span><span class="nx">socket</span> <span class="o">=</span> <span class="nx">socket</span><span class="p">;</span>
+
+ <span class="nx">socket</span><span class="p">.</span><span class="nx">ondata</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">d</span><span class="p">,</span> <span class="nx">start</span><span class="p">,</span> <span class="nx">end</span><span class="p">)</span> <span class="p">{</span>
+ <span class="kd">var</span> <span class="nx">ret</span> <span class="o">=</span> <span class="nx">parser</span><span class="p">.</span><span class="nx">execute</span><span class="p">(</span><span class="nx">d</span><span class="p">,</span> <span class="nx">start</span><span class="p">,</span> <span class="nx">end</span> <span class="o">-</span> <span class="nx">start</span><span class="p">);</span>
+ <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">ret</span><span class="p">);</span>
+ <span class="p">};</span>
+
+ <span class="nx">socket</span><span class="p">.</span><span class="nx">onend</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
+ <span class="kd">var</span> <span class="nx">ret</span> <span class="o">=</span> <span class="nx">parser</span><span class="p">.</span><span class="nx">finish</span><span class="p">();</span>
+
+ <span class="k">if</span> <span class="p">(</span><span class="nx">ret</span> <span class="k">instanceof</span> <span class="nb">Error</span><span class="p">)</span> <span class="p">{</span>
+ <span class="nx">socket</span><span class="p">.</span><span class="nx">destroy</span><span class="p">(</span><span class="nx">ret</span><span class="p">);</span>
+ <span class="k">return</span><span class="p">;</span>
+ <span class="p">}</span>
+
+ <span class="k">if</span> <span class="p">(</span><span class="nx">socket</span><span class="p">.</span><span class="nx">writable</span><span class="p">)</span> <span class="p">{</span>
+ <span class="nx">socket</span><span class="p">.</span><span class="nx">end</span><span class="p">();</span>
+ <span class="p">}</span>
+ <span class="p">};</span>
+
+ <span class="nx">socket</span><span class="p">.</span><span class="nx">write</span><span class="p">(</span><span class="s1">&#39;hello world&#39;</span><span class="p">);</span>
+ <span class="nx">socket</span><span class="p">.</span><span class="nx">end</span><span class="p">();</span>
+ <span class="p">});</span>
+
+ <span class="k">return</span> <span class="nx">server</span><span class="p">;</span>
+<span class="p">};</span>
+
+</pre></div> </td> </tr> </tbody> </table> </div> </body> </html>
View
110 docs/node-http-proxy.html
@@ -99,8 +99,7 @@
</ul> </td> <td class="code"> <div class="highlight"><pre><span class="nx">exports</span><span class="p">.</span><span class="nx">createServer</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">args</span> <span class="o">=</span> <span class="nb">Array</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">slice</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">arguments</span><span class="p">),</span>
<span class="nx">callback</span> <span class="o">=</span> <span class="k">typeof</span> <span class="nx">args</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">===</span> <span class="s1">&#39;function&#39;</span> <span class="o">&amp;&amp;</span> <span class="nx">args</span><span class="p">.</span><span class="nx">shift</span><span class="p">(),</span>
- <span class="nx">options</span> <span class="o">=</span> <span class="p">{},</span>
- <span class="nx">port</span><span class="p">,</span> <span class="nx">host</span><span class="p">,</span> <span class="nx">forward</span><span class="p">,</span> <span class="nx">silent</span><span class="p">,</span> <span class="nx">proxy</span><span class="p">,</span> <span class="nx">server</span><span class="p">;</span>
+ <span class="nx">options</span> <span class="o">=</span> <span class="p">{},</span> <span class="nx">port</span><span class="p">,</span> <span class="nx">host</span><span class="p">,</span> <span class="nx">forward</span><span class="p">,</span> <span class="nx">silent</span><span class="p">,</span> <span class="nx">proxy</span><span class="p">,</span> <span class="nx">server</span><span class="p">;</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">args</span><span class="p">.</span><span class="nx">length</span> <span class="o">&gt;=</span> <span class="mi">2</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">port</span> <span class="o">=</span> <span class="nx">args</span><span class="p">[</span><span class="mi">0</span><span class="p">];</span>
@@ -181,8 +180,10 @@
<span class="kd">var</span> <span class="nx">self</span> <span class="o">=</span> <span class="k">this</span><span class="p">;</span>
<span class="nx">options</span> <span class="o">=</span> <span class="nx">options</span> <span class="o">||</span> <span class="p">{};</span>
+ <span class="nx">options</span><span class="p">.</span><span class="nx">target</span> <span class="o">=</span> <span class="nx">options</span><span class="p">.</span><span class="nx">target</span> <span class="o">||</span> <span class="p">{};</span>
+
<span class="k">this</span><span class="p">.</span><span class="nx">forward</span> <span class="o">=</span> <span class="nx">options</span><span class="p">.</span><span class="nx">forward</span><span class="p">;</span>
- <span class="k">this</span><span class="p">.</span><span class="nx">https</span> <span class="o">=</span> <span class="nx">options</span><span class="p">.</span><span class="nx">https</span><span class="p">;</span>
+ <span class="k">this</span><span class="p">.</span><span class="nx">target</span> <span class="o">=</span> <span class="nx">options</span><span class="p">.</span><span class="nx">target</span><span class="p">;</span>
<span class="k">this</span><span class="p">.</span><span class="nx">changeOrigin</span> <span class="o">=</span> <span class="nx">options</span><span class="p">.</span><span class="nx">changeOrigin</span> <span class="o">||</span> <span class="kc">false</span><span class="p">;</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">options</span><span class="p">.</span><span class="nx">router</span><span class="p">)</span> <span class="p">{</span>
@@ -253,7 +254,11 @@
options.https {Object|boolean} Settings for https.
</code></pre> </td> <td class="code"> <div class="highlight"><pre><span class="nx">HttpProxy</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">proxyRequest</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">req</span><span class="p">,</span> <span class="nx">res</span><span class="p">,</span> <span class="nx">options</span><span class="p">)</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">self</span> <span class="o">=</span> <span class="k">this</span><span class="p">,</span> <span class="nx">errState</span> <span class="o">=</span> <span class="kc">false</span><span class="p">,</span> <span class="nx">location</span><span class="p">,</span> <span class="nx">outgoing</span><span class="p">,</span> <span class="nx">protocol</span><span class="p">,</span> <span class="nx">reverseProxy</span><span class="p">;</span>
- </pre></div> </td> </tr> <tr id="section-20"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-20">&#182;</a> </div> <p>Create an empty options hash if none is passed.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">options</span> <span class="o">=</span> <span class="nx">options</span> <span class="o">||</span> <span class="p">{};</span>
+ </pre></div> </td> </tr> <tr id="section-20"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-20">&#182;</a> </div> <p>Create an empty options hash if none is passed.
+If default options have been passed to the constructor
+of this instance, use them by default.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">options</span> <span class="o">=</span> <span class="nx">options</span> <span class="o">||</span> <span class="p">{};</span>
+ <span class="nx">options</span><span class="p">.</span><span class="nx">host</span> <span class="o">=</span> <span class="nx">options</span><span class="p">.</span><span class="nx">host</span> <span class="o">||</span> <span class="k">this</span><span class="p">.</span><span class="nx">target</span><span class="p">.</span><span class="nx">host</span><span class="p">;</span>
+ <span class="nx">options</span><span class="p">.</span><span class="nx">port</span> <span class="o">=</span> <span class="nx">options</span><span class="p">.</span><span class="nx">port</span> <span class="o">||</span> <span class="k">this</span><span class="p">.</span><span class="nx">target</span><span class="p">.</span><span class="nx">port</span><span class="p">;</span>
</pre></div> </td> </tr> <tr id="section-21"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-21">&#182;</a> </div> <p>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 <code>port</code> and <code>host</code>
@@ -300,7 +305,7 @@
<span class="nx">outgoing</span> <span class="o">=</span> <span class="p">{</span>
<span class="nx">host</span><span class="o">:</span> <span class="nx">options</span><span class="p">.</span><span class="nx">host</span><span class="p">,</span>
<span class="nx">port</span><span class="o">:</span> <span class="nx">options</span><span class="p">.</span><span class="nx">port</span><span class="p">,</span>
- <span class="nx">agent</span><span class="o">:</span> <span class="nx">_getAgent</span><span class="p">(</span><span class="nx">options</span><span class="p">.</span><span class="nx">host</span><span class="p">,</span> <span class="nx">options</span><span class="p">.</span><span class="nx">port</span><span class="p">,</span> <span class="nx">options</span><span class="p">.</span><span class="nx">https</span> <span class="o">||</span> <span class="k">this</span><span class="p">.</span><span class="nx">https</span><span class="p">),</span>
+ <span class="nx">agent</span><span class="o">:</span> <span class="nx">_getAgent</span><span class="p">(</span><span class="nx">options</span><span class="p">.</span><span class="nx">host</span><span class="p">,</span> <span class="nx">options</span><span class="p">.</span><span class="nx">port</span><span class="p">,</span> <span class="nx">options</span><span class="p">.</span><span class="nx">https</span> <span class="o">||</span> <span class="k">this</span><span class="p">.</span><span class="nx">target</span><span class="p">.</span><span class="nx">https</span><span class="p">),</span>
<span class="nx">method</span><span class="o">:</span> <span class="nx">req</span><span class="p">.</span><span class="nx">method</span><span class="p">,</span>
<span class="nx">path</span><span class="o">:</span> <span class="nx">req</span><span class="p">.</span><span class="nx">url</span><span class="p">,</span>
<span class="nx">headers</span><span class="o">:</span> <span class="nx">req</span><span class="p">.</span><span class="nx">headers</span>
@@ -308,7 +313,7 @@
</pre></div> </td> </tr> <tr id="section-28"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-28">&#182;</a> </div> <p>Force the <code>connection</code> header to be 'close' until
node.js core re-implements 'keep-alive'.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">outgoing</span><span class="p">.</span><span class="nx">headers</span><span class="p">[</span><span class="s1">&#39;connection&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;close&#39;</span><span class="p">;</span>
- <span class="nx">protocol</span> <span class="o">=</span> <span class="nx">_getProtocol</span><span class="p">(</span><span class="nx">options</span><span class="p">.</span><span class="nx">https</span> <span class="o">||</span> <span class="k">this</span><span class="p">.</span><span class="nx">https</span><span class="p">,</span> <span class="nx">outgoing</span><span class="p">);</span>
+ <span class="nx">protocol</span> <span class="o">=</span> <span class="nx">_getProtocol</span><span class="p">(</span><span class="nx">options</span><span class="p">.</span><span class="nx">https</span> <span class="o">||</span> <span class="k">this</span><span class="p">.</span><span class="nx">target</span><span class="p">.</span><span class="nx">https</span><span class="p">,</span> <span class="nx">outgoing</span><span class="p">);</span>
</pre></div> </td> </tr> <tr id="section-29"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-29">&#182;</a> </div> <p>Open new HTTP request to internal resource with will act as a reverse proxy pass</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">reverseProxy</span> <span class="o">=</span> <span class="nx">protocol</span><span class="p">.</span><span class="nx">request</span><span class="p">(</span><span class="nx">outgoing</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">response</span><span class="p">)</span> <span class="p">{</span>
</pre></div> </td> </tr> <tr id="section-30"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-30">&#182;</a> </div> <p>Process the <code>reverseProxy</code> <code>response</code> when it's received.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="nx">response</span><span class="p">.</span><span class="nx">headers</span><span class="p">.</span><span class="nx">connection</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">req</span><span class="p">.</span><span class="nx">headers</span><span class="p">.</span><span class="nx">connection</span><span class="p">)</span> <span class="nx">response</span><span class="p">.</span><span class="nx">headers</span><span class="p">.</span><span class="nx">connection</span> <span class="o">=</span> <span class="nx">req</span><span class="p">.</span><span class="nx">headers</span><span class="p">.</span><span class="nx">connection</span><span class="p">;</span>
@@ -402,49 +407,49 @@
<span class="p">}</span>
<span class="p">}</span>
- <span class="kd">function</span> <span class="nx">onUpgrade</span><span class="p">(</span><span class="nx">out</span><span class="p">,</span> <span class="nx">reverseProxy</span><span class="p">)</span> <span class="p">{</span>
- <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">out</span><span class="p">)</span> <span class="p">{</span>
- <span class="nx">reverseProxy</span><span class="p">.</span><span class="nx">end</span><span class="p">();</span>
+ <span class="kd">function</span> <span class="nx">onUpgrade</span><span class="p">(</span><span class="nx">reverseProxy</span><span class="p">,</span> <span class="nx">proxySocket</span><span class="p">)</span> <span class="p">{</span>
+ <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">reverseProxy</span><span class="p">)</span> <span class="p">{</span>
+ <span class="nx">proxySocket</span><span class="p">.</span><span class="nx">end</span><span class="p">();</span>
<span class="nx">socket</span><span class="p">.</span><span class="nx">end</span><span class="p">();</span>
<span class="k">return</span><span class="p">;</span>
<span class="p">}</span>
<span class="kd">var</span> <span class="nx">listeners</span> <span class="o">=</span> <span class="p">{};</span>
- </pre></div> </td> </tr> <tr id="section-50"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-50">&#182;</a> </div> <p>We're now connected to the server, so lets change server socket</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">reverseProxy</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="s1">&#39;data&#39;</span><span class="p">,</span> <span class="nx">listeners</span><span class="p">.</span><span class="nx">_r_data</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">data</span><span class="p">)</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-51"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-51">&#182;</a> </div> <p>Pass data to client</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="nx">out</span><span class="p">.</span><span class="nx">incoming</span><span class="p">.</span><span class="nx">socket</span><span class="p">.</span><span class="nx">writable</span><span class="p">)</span> <span class="p">{</span>
+ </pre></div> </td> </tr> <tr id="section-50"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-50">&#182;</a> </div> <p>We're now connected to the server, so lets change server socket</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">proxySocket</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="s1">&#39;data&#39;</span><span class="p">,</span> <span class="nx">listeners</span><span class="p">.</span><span class="nx">_r_data</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">data</span><span class="p">)</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-51"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-51">&#182;</a> </div> <p>Pass data to client</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="nx">reverseProxy</span><span class="p">.</span><span class="nx">incoming</span><span class="p">.</span><span class="nx">socket</span><span class="p">.</span><span class="nx">writable</span><span class="p">)</span> <span class="p">{</span>
<span class="k">try</span> <span class="p">{</span>
- <span class="nx">out</span><span class="p">.</span><span class="nx">incoming</span><span class="p">.</span><span class="nx">socket</span><span class="p">.</span><span class="nx">write</span><span class="p">(</span><span class="nx">data</span><span class="p">);</span>
+ <span class="nx">reverseProxy</span><span class="p">.</span><span class="nx">incoming</span><span class="p">.</span><span class="nx">socket</span><span class="p">.</span><span class="nx">write</span><span class="p">(</span><span class="nx">data</span><span class="p">);</span>
<span class="p">}</span>
<span class="k">catch</span> <span class="p">(</span><span class="nx">e</span><span class="p">)</span> <span class="p">{</span>
- <span class="nx">out</span><span class="p">.</span><span class="nx">incoming</span><span class="p">.</span><span class="nx">socket</span><span class="p">.</span><span class="nx">end</span><span class="p">();</span>
- <span class="nx">reverseProxy</span><span class="p">.</span><span class="nx">end</span><span class="p">();</span>
+ <span class="nx">reverseProxy</span><span class="p">.</span><span class="nx">incoming</span><span class="p">.</span><span class="nx">socket</span><span class="p">.</span><span class="nx">end</span><span class="p">();</span>
+ <span class="nx">proxySocket</span><span class="p">.</span><span class="nx">end</span><span class="p">();</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="p">});</span>
- <span class="nx">out</span><span class="p">.</span><span class="nx">incoming</span><span class="p">.</span><span class="nx">socket</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="s1">&#39;data&#39;</span><span class="p">,</span> <span class="nx">listeners</span><span class="p">.</span><span class="nx">_data</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">data</span><span class="p">)</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-52"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-52">&#182;</a> </div> <p>Pass data from client to server</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">try</span> <span class="p">{</span>
- <span class="nx">reverseProxy</span><span class="p">.</span><span class="nx">write</span><span class="p">(</span><span class="nx">data</span><span class="p">);</span>
+ <span class="nx">reverseProxy</span><span class="p">.</span><span class="nx">incoming</span><span class="p">.</span><span class="nx">socket</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="s1">&#39;data&#39;</span><span class="p">,</span> <span class="nx">listeners</span><span class="p">.</span><span class="nx">_data</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">data</span><span class="p">)</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-52"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-52">&#182;</a> </div> <p>Pass data from client to server</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">try</span> <span class="p">{</span>
+ <span class="nx">proxySocket</span><span class="p">.</span><span class="nx">write</span><span class="p">(</span><span class="nx">data</span><span class="p">);</span>
<span class="p">}</span>
<span class="k">catch</span> <span class="p">(</span><span class="nx">e</span><span class="p">)</span> <span class="p">{</span>
- <span class="nx">reverseProxy</span><span class="p">.</span><span class="nx">end</span><span class="p">();</span>
+ <span class="nx">proxySocket</span><span class="p">.</span><span class="nx">end</span><span class="p">();</span>
<span class="nx">socket</span><span class="p">.</span><span class="nx">end</span><span class="p">();</span>
<span class="p">}</span>
<span class="p">});</span></pre></div> </td> </tr> <tr id="section-53"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-53">&#182;</a> </div> <p>Detach event listeners from reverseProxy</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">function</span> <span class="nx">detach</span><span class="p">()</span> <span class="p">{</span>
- <span class="nx">reverseProxy</span><span class="p">.</span><span class="nx">removeListener</span><span class="p">(</span><span class="s1">&#39;close&#39;</span><span class="p">,</span> <span class="nx">listeners</span><span class="p">.</span><span class="nx">_r_close</span><span class="p">);</span>
- <span class="nx">reverseProxy</span><span class="p">.</span><span class="nx">removeListener</span><span class="p">(</span><span class="s1">&#39;data&#39;</span><span class="p">,</span> <span class="nx">listeners</span><span class="p">.</span><span class="nx">_r_data</span><span class="p">);</span>
- <span class="nx">out</span><span class="p">.</span><span class="nx">incoming</span><span class="p">.</span><span class="nx">socket</span><span class="p">.</span><span class="nx">removeListener</span><span class="p">(</span><span class="s1">&#39;data&#39;</span><span class="p">,</span> <span class="nx">listeners</span><span class="p">.</span><span class="nx">_data</span><span class="p">);</span>
- <span class="nx">out</span><span class="p">.</span><span class="nx">incoming</span><span class="p">.</span><span class="nx">socket</span><span class="p">.</span><span class="nx">removeListener</span><span class="p">(</span><span class="s1">&#39;close&#39;</span><span class="p">,</span> <span class="nx">listeners</span><span class="p">.</span><span class="nx">_close</span><span class="p">);</span>
- <span class="p">}</span></pre></div> </td> </tr> <tr id="section-54"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-54">&#182;</a> </div> <p>Hook disconnections</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">reverseProxy</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="s1">&#39;end&#39;</span><span class="p">,</span> <span class="nx">listeners</span><span class="p">.</span><span class="nx">_r_close</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
- <span class="nx">out</span><span class="p">.</span><span class="nx">incoming</span><span class="p">.</span><span class="nx">socket</span><span class="p">.</span><span class="nx">end</span><span class="p">();</span>
+ <span class="nx">proxySocket</span><span class="p">.</span><span class="nx">removeListener</span><span class="p">(</span><span class="s1">&#39;end&#39;</span><span class="p">,</span> <span class="nx">listeners</span><span class="p">.</span><span class="nx">_r_close</span><span class="p">);</span>
+ <span class="nx">proxySocket</span><span class="p">.</span><span class="nx">removeListener</span><span class="p">(</span><span class="s1">&#39;data&#39;</span><span class="p">,</span> <span class="nx">listeners</span><span class="p">.</span><span class="nx">_r_data</span><span class="p">);</span>
+ <span class="nx">reverseProxy</span><span class="p">.</span><span class="nx">incoming</span><span class="p">.</span><span class="nx">socket</span><span class="p">.</span><span class="nx">removeListener</span><span class="p">(</span><span class="s1">&#39;data&#39;</span><span class="p">,</span> <span class="nx">listeners</span><span class="p">.</span><span class="nx">_data</span><span class="p">);</span>
+ <span class="nx">reverseProxy</span><span class="p">.</span><span class="nx">incoming</span><span class="p">.</span><span class="nx">socket</span><span class="p">.</span><span class="nx">removeListener</span><span class="p">(</span><span class="s1">&#39;end&#39;</span><span class="p">,</span> <span class="nx">listeners</span><span class="p">.</span><span class="nx">_close</span><span class="p">);</span>
+ <span class="p">}</span></pre></div> </td> </tr> <tr id="section-54"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-54">&#182;</a> </div> <p>Hook disconnections</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">proxySocket</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="s1">&#39;end&#39;</span><span class="p">,</span> <span class="nx">listeners</span><span class="p">.</span><span class="nx">_r_close</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
+ <span class="nx">reverseProxy</span><span class="p">.</span><span class="nx">incoming</span><span class="p">.</span><span class="nx">socket</span><span class="p">.</span><span class="nx">end</span><span class="p">();</span>
<span class="nx">detach</span><span class="p">();</span>
<span class="p">});</span>
- <span class="nx">socket</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="s1">&#39;end&#39;</span><span class="p">,</span> <span class="nx">listeners</span><span class="p">.</span><span class="nx">_close</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
- <span class="nx">reverseProxy</span><span class="p">.</span><span class="nx">end</span><span class="p">();</span>
+ <span class="nx">reverseProxy</span><span class="p">.</span><span class="nx">incoming</span><span class="p">.</span><span class="nx">socket</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="s1">&#39;end&#39;</span><span class="p">,</span> <span class="nx">listeners</span><span class="p">.</span><span class="nx">_close</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
+ <span class="nx">proxySocket</span><span class="p">.</span><span class="nx">end</span><span class="p">();</span>
<span class="nx">detach</span><span class="p">();</span>
<span class="p">});</span>
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-55"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-55">&#182;</a> </div> <p>Client socket</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_socket</span><span class="p">(</span><span class="nx">socket</span><span class="p">);</span>
- </pre></div> </td> </tr> <tr id="section-56"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-56">&#182;</a> </div> <p>Remote host address</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">protocolName</span> <span class="o">=</span> <span class="nx">options</span><span class="p">.</span><span class="nx">https</span> <span class="o">||</span> <span class="k">this</span><span class="p">.</span><span class="nx">https</span> <span class="o">?</span> <span class="s1">&#39;https&#39;</span> <span class="o">:</span> <span class="s1">&#39;http&#39;</span><span class="p">,</span>
- <span class="nx">agent</span> <span class="o">=</span> <span class="nx">_getAgent</span><span class="p">(</span><span class="nx">options</span><span class="p">.</span><span class="nx">host</span><span class="p">,</span> <span class="nx">options</span><span class="p">.</span><span class="nx">port</span><span class="p">,</span> <span class="nx">options</span><span class="p">.</span><span class="nx">https</span> <span class="o">||</span> <span class="k">this</span><span class="p">.</span><span class="nx">https</span><span class="p">),</span>
+ </pre></div> </td> </tr> <tr id="section-56"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-56">&#182;</a> </div> <p>Remote host address</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">protocolName</span> <span class="o">=</span> <span class="nx">options</span><span class="p">.</span><span class="nx">https</span> <span class="o">||</span> <span class="k">this</span><span class="p">.</span><span class="nx">target</span><span class="p">.</span><span class="nx">https</span> <span class="o">?</span> <span class="s1">&#39;https&#39;</span> <span class="o">:</span> <span class="s1">&#39;http&#39;</span><span class="p">,</span>
+ <span class="nx">agent</span> <span class="o">=</span> <span class="nx">_getAgent</span><span class="p">(</span><span class="nx">options</span><span class="p">.</span><span class="nx">host</span><span class="p">,</span> <span class="nx">options</span><span class="p">.</span><span class="nx">port</span><span class="p">,</span> <span class="nx">options</span><span class="p">.</span><span class="nx">https</span> <span class="o">||</span> <span class="k">this</span><span class="p">.</span><span class="nx">target</span><span class="p">.</span><span class="nx">https</span><span class="p">),</span>
<span class="nx">remoteHost</span> <span class="o">=</span> <span class="nx">options</span><span class="p">.</span><span class="nx">host</span> <span class="o">+</span> <span class="p">(</span><span class="nx">options</span><span class="p">.</span><span class="nx">port</span> <span class="o">-</span> <span class="mi">80</span> <span class="o">===</span> <span class="mi">0</span> <span class="o">?</span> <span class="s1">&#39;&#39;</span> <span class="o">:</span> <span class="s1">&#39;:&#39;</span> <span class="o">+</span> <span class="nx">options</span><span class="p">.</span><span class="nx">port</span><span class="p">);</span></pre></div> </td> </tr> <tr id="section-57"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-57">&#182;</a> </div> <p>Change headers</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">changeOrigin</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">req</span><span class="p">.</span><span class="nx">headers</span><span class="p">.</span><span class="nx">host</span> <span class="o">=</span> <span class="nx">remoteHost</span><span class="p">;</span>
<span class="nx">req</span><span class="p">.</span><span class="nx">headers</span><span class="p">.</span><span class="nx">origin</span> <span class="o">=</span> <span class="nx">protocolName</span> <span class="o">+</span> <span class="s1">&#39;://&#39;</span> <span class="o">+</span> <span class="nx">remoteHost</span><span class="p">;</span>
@@ -456,11 +461,11 @@
<span class="nx">method</span><span class="o">:</span> <span class="s1">&#39;GET&#39;</span><span class="p">,</span>
<span class="nx">path</span><span class="o">:</span> <span class="nx">req</span><span class="p">.</span><span class="nx">url</span><span class="p">,</span>
<span class="nx">headers</span><span class="o">:</span> <span class="nx">req</span><span class="p">.</span><span class="nx">headers</span><span class="p">,</span>
- <span class="p">};</span></pre></div> </td> </tr> <tr id="section-58"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-58">&#182;</a> </div> <p>Make the outgoing WebSocket request</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">request</span> <span class="o">=</span> <span class="nx">agent</span><span class="p">.</span><span class="nx">appendMessage</span><span class="p">(</span><span class="nx">outgoing</span><span class="p">);</span></pre></div> </td> </tr> <tr id="section-59"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-59">&#182;</a> </div> <p>Here we set the incoming <code>req</code>, <code>socket</code> and <code>head</code> data to the outgoing
+ <span class="p">};</span></pre></div> </td> </tr> <tr id="section-58"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-58">&#182;</a> </div> <p>Make the outgoing WebSocket request</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">reverseProxy</span> <span class="o">=</span> <span class="nx">agent</span><span class="p">.</span><span class="nx">appendMessage</span><span class="p">(</span><span class="nx">outgoing</span><span class="p">);</span></pre></div> </td> </tr> <tr id="section-59"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-59">&#182;</a> </div> <p>Here we set the incoming <code>req</code>, <code>socket</code> and <code>head</code> data to the outgoing
request so that we can reuse this data later on in the closure scope
available to the <code>upgrade</code> event. This bookkeeping is not tracked anywhere
-in nodejs core and is <strong>very</strong> specific to proxying WebSockets.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">request</span><span class="p">.</span><span class="nx">agent</span> <span class="o">=</span> <span class="nx">agent</span><span class="p">;</span>
- <span class="nx">request</span><span class="p">.</span><span class="nx">incoming</span> <span class="o">=</span> <span class="p">{</span>
+in nodejs core and is <strong>very</strong> specific to proxying WebSockets.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">reverseProxy</span><span class="p">.</span><span class="nx">agent</span> <span class="o">=</span> <span class="nx">agent</span><span class="p">;</span>
+ <span class="nx">reverseProxy</span><span class="p">.</span><span class="nx">incoming</span> <span class="o">=</span> <span class="p">{</span>
<span class="nx">request</span><span class="o">:</span> <span class="nx">req</span><span class="p">,</span>
<span class="nx">socket</span><span class="o">:</span> <span class="nx">socket</span><span class="p">,</span>
<span class="nx">head</span><span class="o">:</span> <span class="nx">head</span>
@@ -471,37 +476,46 @@
<p>In addition, it's important to note the closure scope here. Since
there is no mapping of the </p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">agent</span><span class="p">.</span><span class="nx">_events</span> <span class="o">||</span> <span class="nx">agent</span><span class="p">.</span><span class="nx">_events</span><span class="p">[</span><span class="s1">&#39;upgrade&#39;</span><span class="p">].</span><span class="nx">length</span> <span class="o">===</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
- <span class="nx">agent</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="s1">&#39;upgrade&#39;</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">out</span><span class="p">,</span> <span class="nx">remoteSocket</span><span class="p">,</span> <span class="nx">head</span><span class="p">)</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-61"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-61">&#182;</a> </div> <p>Prepare socket</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_socket</span><span class="p">(</span><span class="nx">remoteSocket</span><span class="p">,</span> <span class="kc">true</span><span class="p">);</span>
- </pre></div> </td> </tr> <tr id="section-62"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-62">&#182;</a> </div> <p>Emit event</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">onUpgrade</span><span class="p">(</span><span class="nx">remoteSocket</span><span class="p">.</span><span class="nx">_httpMessage</span><span class="p">,</span> <span class="nx">remoteSocket</span><span class="p">);</span>
+ <span class="nx">agent</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="s1">&#39;upgrade&#39;</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">_</span><span class="p">,</span> <span class="nx">remoteSocket</span><span class="p">,</span> <span class="nx">head</span><span class="p">)</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-61"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-61">&#182;</a> </div> <p>Prepare the socket for the reverseProxy request and begin to
+stream data between the two sockets</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_socket</span><span class="p">(</span><span class="nx">remoteSocket</span><span class="p">,</span> <span class="kc">true</span><span class="p">);</span>
+ <span class="nx">onUpgrade</span><span class="p">(</span><span class="nx">remoteSocket</span><span class="p">.</span><span class="nx">_httpMessage</span><span class="p">,</span> <span class="nx">remoteSocket</span><span class="p">);</span>
<span class="p">});</span>
<span class="p">}</span>
-
- <span class="k">if</span> <span class="p">(</span><span class="k">typeof</span> <span class="nx">request</span><span class="p">.</span><span class="nx">socket</span> <span class="o">!==</span> <span class="s1">&#39;undefined&#39;</span><span class="p">)</span> <span class="p">{</span>
- <span class="nx">request</span><span class="p">.</span><span class="nx">socket</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="s1">&#39;data&#39;</span><span class="p">,</span> <span class="kd">function</span> <span class="nx">handshake</span> <span class="p">(</span><span class="nx">data</span><span class="p">)</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-63"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-63">&#182;</a> </div> <p>Handshaking</p> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-64"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-64">&#182;</a> </div> <p>Ok, kind of harmfull part of code
-Socket.IO is sending hash at the end of handshake
-If protocol = 76
-But we need to replace 'host' and 'origin' in response
-So we split data to printable data and to non-printable
-(Non-printable will come after double-CRLF)</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">sdata</span> <span class="o">=</span> <span class="nx">data</span><span class="p">.</span><span class="nx">toString</span><span class="p">();</span></pre></div> </td> </tr> <tr id="section-65"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-65">&#182;</a> </div> <p>Get Printable</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">sdata</span> <span class="o">=</span> <span class="nx">sdata</span><span class="p">.</span><span class="nx">substr</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="nx">sdata</span><span class="p">.</span><span class="nx">search</span><span class="p">(</span><span class="nx">CRLF</span> <span class="o">+</span> <span class="nx">CRLF</span><span class="p">));</span></pre></div> </td> </tr> <tr id="section-66"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-66">&#182;</a> </div> <p>Get Non-Printable</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">data</span> <span class="o">=</span> <span class="nx">data</span><span class="p">.</span><span class="nx">slice</span><span class="p">(</span><span class="nx">Buffer</span><span class="p">.</span><span class="nx">byteLength</span><span class="p">(</span><span class="nx">sdata</span><span class="p">),</span> <span class="nx">data</span><span class="p">.</span><span class="nx">length</span><span class="p">);</span></pre></div> </td> </tr> <tr id="section-67"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-67">&#182;</a> </div> <p>Replace host and origin</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">sdata</span> <span class="o">=</span> <span class="nx">sdata</span><span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="nx">remoteHost</span><span class="p">,</span> <span class="nx">options</span><span class="p">.</span><span class="nx">host</span><span class="p">)</span>
+ </pre></div> </td> </tr> <tr id="section-62"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-62">&#182;</a> </div> <p>If the reverseProxy connection has an underlying socket,
+then behing the handshake.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="k">typeof</span> <span class="nx">reverseProxy</span><span class="p">.</span><span class="nx">socket</span> <span class="o">!==</span> <span class="s1">&#39;undefined&#39;</span><span class="p">)</span> <span class="p">{</span>
+ <span class="nx">reverseProxy</span><span class="p">.</span><span class="nx">socket</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="s1">&#39;data&#39;</span><span class="p">,</span> <span class="kd">function</span> <span class="nx">handshake</span> <span class="p">(</span><span class="nx">data</span><span class="p">)</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-63"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-63">&#182;</a> </div> <p>Ok, kind of harmfull part of code. Socket.IO sends a hash
+at the end of handshake if protocol === 76, but we need
+to replace 'host' and 'origin' in response so we split
+data to printable data and to non-printable. (Non-printable
+will come after double-CRLF).</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">sdata</span> <span class="o">=</span> <span class="nx">data</span><span class="p">.</span><span class="nx">toString</span><span class="p">();</span></pre></div> </td> </tr> <tr id="section-64"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-64">&#182;</a> </div> <p>Get the Printable data</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">sdata</span> <span class="o">=</span> <span class="nx">sdata</span><span class="p">.</span><span class="nx">substr</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="nx">sdata</span><span class="p">.</span><span class="nx">search</span><span class="p">(</span><span class="nx">CRLF</span> <span class="o">+</span> <span class="nx">CRLF</span><span class="p">));</span></pre></div> </td> </tr> <tr id="section-65"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-65">&#182;</a> </div> <p>Get the Non-Printable data</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">data</span> <span class="o">=</span> <span class="nx">data</span><span class="p">.</span><span class="nx">slice</span><span class="p">(</span><span class="nx">Buffer</span><span class="p">.</span><span class="nx">byteLength</span><span class="p">(</span><span class="nx">sdata</span><span class="p">),</span> <span class="nx">data</span><span class="p">.</span><span class="nx">length</span><span class="p">);</span></pre></div> </td> </tr> <tr id="section-66"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-66">&#182;</a> </div> <p>Replace the host and origin headers in the Printable data</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">sdata</span> <span class="o">=</span> <span class="nx">sdata</span><span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="nx">remoteHost</span><span class="p">,</span> <span class="nx">options</span><span class="p">.</span><span class="nx">host</span><span class="p">)</span>
<span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="nx">remoteHost</span><span class="p">,</span> <span class="nx">options</span><span class="p">.</span><span class="nx">host</span><span class="p">);</span>
- <span class="k">try</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-68"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-68">&#182;</a> </div> <p>Write printable</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">socket</span><span class="p">.</span><span class="nx">write</span><span class="p">(</span><span class="nx">sdata</span><span class="p">);</span></pre></div> </td> </tr> <tr id="section-69"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-69">&#182;</a> </div> <p>Write non-printable</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">socket</span><span class="p">.</span><span class="nx">write</span><span class="p">(</span><span class="nx">data</span><span class="p">);</span>
+ <span class="k">try</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-67"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-67">&#182;</a> </div> <p>Write the printable and non-printable data to the socket
+from the original incoming request. </p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">socket</span><span class="p">.</span><span class="nx">write</span><span class="p">(</span><span class="nx">sdata</span><span class="p">);</span>
+ <span class="nx">socket</span><span class="p">.</span><span class="nx">write</span><span class="p">(</span><span class="nx">data</span><span class="p">);</span>
<span class="p">}</span>
<span class="k">catch</span> <span class="p">(</span><span class="nx">e</span><span class="p">)</span> <span class="p">{</span>
- <span class="nx">request</span><span class="p">.</span><span class="nx">end</span><span class="p">();</span>
+ <span class="nx">reverseProxy</span><span class="p">.</span><span class="nx">end</span><span class="p">();</span>
<span class="nx">socket</span><span class="p">.</span><span class="nx">end</span><span class="p">();</span>
- <span class="p">}</span></pre></div> </td> </tr> <tr id="section-70"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-70">&#182;</a> </div> <p>Catch socket errors</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">socket</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="s1">&#39;error&#39;</span><span class="p">,</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
- <span class="nx">request</span><span class="p">.</span><span class="nx">end</span><span class="p">();</span>
- <span class="p">});</span></pre></div> </td> </tr> <tr id="section-71"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-71">&#182;</a> </div> <p>Remove data listener now that the 'handshake' is complete</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">request</span><span class="p">.</span><span class="nx">socket</span><span class="p">.</span><span class="nx">removeListener</span><span class="p">(</span><span class="s1">&#39;data&#39;</span><span class="p">,</span> <span class="nx">handshake</span><span class="p">);</span>
+ <span class="p">}</span></pre></div> </td> </tr> <tr id="section-68"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-68">&#182;</a> </div> <p>Catch socket errors</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">socket</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="s1">&#39;error&#39;</span><span class="p">,</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
+ <span class="nx">reverseProxy</span><span class="p">.</span><span class="nx">end</span><span class="p">();</span>
+ <span class="nx">socket</span><span class="p">.</span><span class="nx">end</span><span class="p">();</span>
+ <span class="p">});</span></pre></div> </td> </tr> <tr id="section-69"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-69">&#182;</a> </div> <p>Remove data listener now that the 'handshake' is complete</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">reverseProxy</span><span class="p">.</span><span class="nx">socket</span><span class="p">.</span><span class="nx">removeListener</span><span class="p">(</span><span class="s1">&#39;data&#39;</span><span class="p">,</span> <span class="nx">handshake</span><span class="p">);</span>
<span class="p">});</span>
- <span class="p">}</span></pre></div> </td> </tr> <tr id="section-72"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-72">&#182;</a> </div> <p>Write upgrade-head</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">try</span> <span class="p">{</span>
- <span class="nx">request</span><span class="p">.</span><span class="nx">write</span><span class="p">(</span><span class="nx">head</span><span class="p">);</span>
+ <span class="p">}</span>
+
+ <span class="nx">reverseProxy</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="s1">&#39;error&#39;</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span>
+ <span class="nx">reverseProxy</span><span class="p">.</span><span class="nx">end</span><span class="p">();</span>
+ <span class="nx">socket</span><span class="p">.</span><span class="nx">end</span><span class="p">();</span>
+ <span class="p">});</span>
+
+ <span class="k">try</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-70"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-70">&#182;</a> </div> <p>Attempt to write the upgrade-head to the reverseProxy request.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">reverseProxy</span><span class="p">.</span><span class="nx">write</span><span class="p">(</span><span class="nx">head</span><span class="p">);</span>
<span class="p">}</span>
<span class="k">catch</span> <span class="p">(</span><span class="nx">ex</span><span class="p">)</span> <span class="p">{</span>
- <span class="nx">request</span><span class="p">.</span><span class="nx">end</span><span class="p">();</span>
+ <span class="nx">reverseProxy</span><span class="p">.</span><span class="nx">end</span><span class="p">();</span>
<span class="nx">socket</span><span class="p">.</span><span class="nx">end</span><span class="p">();</span>
<span class="p">}</span>
- </pre></div> </td> </tr> <tr id="section-73"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-73">&#182;</a> </div> <p>If we have been passed buffered data, resume it.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="nx">options</span><span class="p">.</span><span class="nx">buffer</span> <span class="o">&amp;&amp;</span> <span class="o">!</span><span class="nx">errState</span><span class="p">)</span> <span class="p">{</span>
+ </pre></div> </td> </tr> <tr id="section-71"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-71">&#182;</a> </div> <p>If we have been passed buffered data, resume it.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="nx">options</span><span class="p">.</span><span class="nx">buffer</span> <span class="o">&amp;&amp;</span> <span class="o">!</span><span class="nx">errState</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">options</span><span class="p">.</span><span class="nx">buffer</span><span class="p">.</span><span class="nx">resume</span><span class="p">();</span>
<span class="p">}</span>
<span class="p">};</span>
View
53 examples/proxy-https-to-http.js
@@ -0,0 +1,53 @@
+/*
+ proxy-https-to-http.js: Basic example of proxying over HTTPS to a target HTTP server
+
+ Copyright (c) 2010 Charlie Robbins, Mikeal Rogers, Fedor Indutny, & Marak Squires.
+
+ 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 https = require('https'),
+ http = require('http'),
+ util = require('util'),
+ colors = require('colors'),
+ httpProxy = require('./../lib/node-http-proxy'),
+ helpers = require('./../test/helpers');
+
+var opts = helpers.loadHttps();
+
+//
+// Crete the target HTTPS server
+//
+http.createServer(function (req, res) {
+ res.writeHead(200, { 'Content-Type': 'text/plain' });
+ res.write('hello http over https\n');
+ res.end();
+}).listen(8000);
+
+//
+// Create the proxy server listening on port 443.
+//
+httpProxy.createServer(8000, 'localhost', {
+ https: opts
+}).listen(8080);
+
+util.puts('https proxy server'.blue + ' started '.green.bold + 'on port '.blue + '8080'.yellow);
+util.puts('http server '.blue + 'started '.green.bold + 'on port '.blue + '8000 '.yellow);
View
13 examples/basic-proxy-https.js → examples/proxy-https-to-https.js
@@ -1,5 +1,5 @@
/*
- basic-proxy-https.js: Basic example of proxying over HTTPS
+ proxy-https-to-https.js: Basic example of proxying over HTTPS to a target HTTPS server
Copyright (c) 2010 Charlie Robbins, Mikeal Rogers, Fedor Indutny, & Marak Squires.
@@ -45,9 +45,12 @@ https.createServer(opts, function (req, res) {
//
// Create the proxy server listening on port 443.
//
-httpProxy.createServer(443, 'localhost', {
- https: opts
+httpProxy.createServer(8000, 'localhost', {
+ https: opts,
+ target: {
+ https: true
+ }
}).listen(8080);
-util.puts('https proxy server'.blue + ' started '.green.bold + 'on port '.blue + '8000'.yellow);
-util.puts('https server '.blue + 'started '.green.bold + 'on port '.blue + '8080 '.yellow);
+util.puts('https proxy server'.blue + ' started '.green.bold + 'on port '.blue + '8080'.yellow);
+util.puts('https server '.blue + 'started '.green.bold + 'on port '.blue + '8000 '.yellow);
View
179 lib/node-http-proxy.js
@@ -113,8 +113,7 @@ exports.setMaxSockets = function (value) {
exports.createServer = function () {
var args = Array.prototype.slice.call(arguments),
callback = typeof args[0] === 'function' && args.shift(),
- options = {},
- port, host, forward, silent, proxy, server;
+ options = {}, port, host, forward, silent, proxy, server;
if (args.length >= 2) {
port = args[0];
@@ -221,8 +220,10 @@ var HttpProxy = exports.HttpProxy = function (options) {
var self = this;
options = options || {};
+ options.target = options.target || {};
+
this.forward = options.forward;
- this.https = options.https;
+ this.target = options.target;
this.changeOrigin = options.changeOrigin || false;
if (options.router) {
@@ -303,8 +304,14 @@ HttpProxy.prototype.close = function () {
HttpProxy.prototype.proxyRequest = function (req, res, options) {
var self = this, errState = false, location, outgoing, protocol, reverseProxy;
+ //
// Create an empty options hash if none is passed.
- options = options || {};
+ // If default options have been passed to the constructor
+ // of this instance, use them by default.
+ //
+ options = options || {};
+ options.host = options.host || this.target.host;
+ options.port = options.port || this.target.port;
//
// Check the proxy table for this instance to see if we need
@@ -337,9 +344,16 @@ HttpProxy.prototype.proxyRequest = function (req, res, options) {
}
//
- // Add `x-forwarded-for` header to availible client IP to apps behind proxy
+ // Add common proxy headers to the request so that they can
+ // be availible to the proxy target server:
+ //
+ // * `x-forwarded-for`: IP Address of the original request
+ // * `x-forwarded-proto`: Protocol of the original request
+ // * `x-forwarded-port`: Port of the original request.
//
- req.headers['x-forwarded-for'] = req.connection.remoteAddress;
+ req.headers['x-forwarded-for'] = req.connection.remoteAddress || req.connection.socket.remoteAddress;
+ req.headers['x-forwarded-port'] = req.connection.remotePort || req.connection.socket.remotePort;
+ req.headers['x-forwarded-proto'] = res.connection.pair ? 'https' : 'http';
//
// Emit the `start` event indicating that we have begun the proxy operation.
@@ -363,10 +377,28 @@ HttpProxy.prototype.proxyRequest = function (req, res, options) {
//
function proxyError(err) {
errState = true;
+
+ //
+ // Emit an `error` event, allowing the application to use custom
+ // error handling. The error handler should end the response.
+ //
+ if (self.emit('proxyError', err, req, res)) {
+ return;
+ }
+
res.writeHead(500, { 'Content-Type': 'text/plain' });
if (req.method !== 'HEAD') {
- res.write('An error has occurred: ' + JSON.stringify(err));
+ //
+ // This NODE_ENV=production behavior is mimics Express and
+ // Connect.
+ //
+ if (process.env.NODE_ENV === 'production') {
+ res.write('Internal Server Error');
+ }
+ else {
+ res.write('An error has occurred: ' + JSON.stringify(err));
+ }
}
res.end();
@@ -375,7 +407,7 @@ HttpProxy.prototype.proxyRequest = function (req, res, options) {
outgoing = {
host: options.host,
port: options.port,
- agent: _getAgent(options.host, options.port, options.https || this.https),
+ agent: _getAgent(options.host, options.port, options.https || this.target.https),
method: req.method,
path: req.url,
headers: req.headers
@@ -385,7 +417,7 @@ HttpProxy.prototype.proxyRequest = function (req, res, options) {
// node.js core re-implements 'keep-alive'.
outgoing.headers['connection'] = 'close';
- protocol = _getProtocol(options.https || this.https, outgoing);
+ protocol = _getProtocol(options.https || this.target.https, outgoing);
// Open new HTTP request to internal resource with will act as a reverse proxy pass
reverseProxy = protocol.request(outgoing, function (response) {
@@ -516,7 +548,7 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options
// This request is not WebSocket request
return;
}
-
+
// Turn of all bufferings
// For server set KeepAlive
// For client set encoding
@@ -536,9 +568,9 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options
}
}
- function onUpgrade(out, reverseProxy) {
- if (!out) {
- reverseProxy.end();
+ function onUpgrade(reverseProxy, proxySocket) {
+ if (!reverseProxy) {
+ proxySocket.end();
socket.end();
return;
}
@@ -546,46 +578,46 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options
var listeners = {};
// We're now connected to the server, so lets change server socket
- reverseProxy.on('data', listeners._r_data = function(data) {
+ proxySocket.on('data', listeners._r_data = function(data) {
// Pass data to client
- if (out.incoming.socket.writable) {
+ if (reverseProxy.incoming.socket.writable) {
try {
- out.incoming.socket.write(data);
+ reverseProxy.incoming.socket.write(data);
}
catch (e) {
- out.incoming.socket.end();
- reverseProxy.end();
+ reverseProxy.incoming.socket.end();
+ proxySocket.end();
}
}
});
- out.incoming.socket.on('data', listeners._data = function(data) {
+ reverseProxy.incoming.socket.on('data', listeners._data = function(data) {
// Pass data from client to server
try {
- reverseProxy.write(data);
+ proxySocket.write(data);
}
catch (e) {
- reverseProxy.end();
+ proxySocket.end();
socket.end();
}
});
// Detach event listeners from reverseProxy
function detach() {
- reverseProxy.removeListener('close', listeners._r_close);
- reverseProxy.removeListener('data', listeners._r_data);
- out.incoming.socket.removeListener('data', listeners._data);
- out.incoming.socket.removeListener('close', listeners._close);
+ proxySocket.removeListener('end', listeners._r_close);
+ proxySocket.removeListener('data', listeners._r_data);
+ reverseProxy.incoming.socket.removeListener('data', listeners._data);
+ reverseProxy.incoming.socket.removeListener('end', listeners._close);
}
// Hook disconnections
- reverseProxy.on('end', listeners._r_close = function() {
- out.incoming.socket.end();
+ proxySocket.on('end', listeners._r_close = function() {
+ reverseProxy.incoming.socket.end();
detach();
});
- socket.on('end', listeners._close = function() {
- reverseProxy.end();
+ reverseProxy.incoming.socket.on('end', listeners._close = function() {
+ proxySocket.end();
detach();
});
};
@@ -594,8 +626,8 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options
_socket(socket);
// Remote host address
- var protocolName = options.https || this.https ? 'https' : 'http',
- agent = _getAgent(options.host, options.port, options.https || this.https),
+ var protocolName = options.https || this.target.https ? 'https' : 'http',
+ agent = _getAgent(options.host, options.port, options.https || this.target.https),
remoteHost = options.host + (options.port - 80 === 0 ? '' : ':' + options.port);
// Change headers
@@ -613,7 +645,16 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options
};
// Make the outgoing WebSocket request
- var request = agent.appendMessage(outgoing);
+ var reverseProxy = agent.appendMessage(outgoing);
+
+ function proxyError (err) {
+ reverseProxy.end();
+ if (self.emit('webSocketProxyError', req, socket, head)) {
+ return;
+ }
+
+ socket.end();
+ }
//
// Here we set the incoming `req`, `socket` and `head` data to the outgoing
@@ -621,8 +662,8 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options
// available to the `upgrade` event. This bookkeeping is not tracked anywhere
// in nodejs core and is **very** specific to proxying WebSockets.
//
- request.agent = agent;
- request.incoming = {
+ reverseProxy.agent = agent;
+ reverseProxy.incoming = {
request: req,
socket: socket,
head: head
@@ -637,69 +678,77 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options
// there is no mapping of the
//
if (!agent._events || agent._events['upgrade'].length === 0) {
- agent.on('upgrade', function (out, remoteSocket, head) {
- // Prepare socket
+ agent.on('upgrade', function (_, remoteSocket, head) {
+ //
+ // Prepare the socket for the reverseProxy request and begin to
+ // stream data between the two sockets. Here it is important to
+ // note that `remoteSocket._httpMessage === reverseProxy`.
+ //
_socket(remoteSocket, true);
-
- // Emit event
onUpgrade(remoteSocket._httpMessage, remoteSocket);
});
}
- if (typeof request.socket !== 'undefined') {
- request.socket.on('data', function handshake (data) {
- // Handshaking
-
- // Ok, kind of harmfull part of code
- // Socket.IO is sending hash at the end of handshake
- // If protocol = 76
- // But we need to replace 'host' and 'origin' in response
- // So we split data to printable data and to non-printable
- // (Non-printable will come after double-CRLF)
+ //
+ // If the reverseProxy connection has an underlying socket,
+ // then behing the handshake.
+ //
+ if (typeof reverseProxy.socket !== 'undefined') {
+ reverseProxy.socket.on('data', function handshake (data) {
+ //
+ // Ok, kind of harmfull part of code. Socket.IO sends a hash
+ // at the end of handshake if protocol === 76, but we need
+ // to replace 'host' and 'origin' in response so we split
+ // data to printable data and to non-printable. (Non-printable
+ // will come after double-CRLF).
+ //
var sdata = data.toString();
- // Get Printable
+ // Get the Printable data
sdata = sdata.substr(0, sdata.search(CRLF + CRLF));
- // Get Non-Printable
+ // Get the Non-Printable data
data = data.slice(Buffer.byteLength(sdata), data.length);
- // Replace host and origin
+ // Replace the host and origin headers in the Printable data
sdata = sdata.replace(remoteHost, options.host)
.replace(remoteHost, options.host);
try {
- // Write printable
+ //
+ // Write the printable and non-printable data to the socket
+ // from the original incoming request.
+ //
socket.write(sdata);
-
- // Write non-printable
socket.write(data);
}
- catch (e) {
- request.end();
- socket.end();
+ catch (ex) {
+ proxyError(ex)
}
// Catch socket errors
- socket.on('error', function() {
- request.end();
- });
+ socket.on('error', proxyError);
// Remove data listener now that the 'handshake' is complete
- request.socket.removeListener('data', handshake);
+ reverseProxy.socket.removeListener('data', handshake);
});
}
+
+ reverseProxy.on('error', proxyError);
- // Write upgrade-head
try {
- request.write(head);
+ //
+ // Attempt to write the upgrade-head to the reverseProxy request.
+ //
+ reverseProxy.write(head);
}
catch (ex) {
- request.end();
- socket.end();
+ proxyError(ex);
}
+ //
// If we have been passed buffered data, resume it.
+ //
if (options.buffer && !errState) {
options.buffer.resume();
}
View
4 package.json
@@ -1,7 +1,7 @@
{
"name": "http-proxy",
"description": "A full-featured http reverse proxy for node.js",
- "version": "0.5.3",
+ "version": "0.5.7",
"author": "Charlie Robbins <charlie.robbins@gmail.com>",
"contributors": [
{ "name": "Mikeal Rogers", "email": "mikeal.rogers@gmail.com" },
@@ -25,6 +25,6 @@
},
"main": "./lib/node-http-proxy",
"bin": { "node-http-proxy": "./bin/node-http-proxy" },
- "scripts": { "test": "vows test/*-test.js --spec" },
+ "scripts": { "test": "vows test/*-test.js --spec && vows test/*-test.js --spec --https" },
"engines": { "node": "= 0.4.7" }
}
View
20 test/helpers.js
@@ -33,14 +33,22 @@ var loadHttps = exports.loadHttps = function () {
};
};
-var TestRunner = exports.TestRunner = function (protocol) {
- this.options = {};
- this.protocol = protocol;
- this.testServers = [];
+var TestRunner = exports.TestRunner = function (protocol, target) {
+ this.options = {};
+ this.options.target = {};
+ this.protocol = protocol;
+ this.target = target;
+ this.testServers = [];
if (protocol === 'https') {
this.options.https = loadHttps();
}
+
+ if (target === 'https') {
+ this.options.target = {
+ https: loadHttps()
+ };
+ }
};
TestRunner.prototype.assertProxied = function (host, proxyPort, port, createProxy) {
@@ -213,8 +221,8 @@ TestRunner.prototype.startTargetServer = function (port, output, callback) {
res.end();
};
- targetServer = this.options.https
- ? https.createServer(this.options.https, handler)
+ targetServer = this.options.target.https
+ ? https.createServer(this.options.target.https, handler)
: http.createServer(handler);
targetServer.listen(port, function () {
View
3 test/node-http-proxy-test.js
@@ -46,7 +46,8 @@ var badForwardOptions = {
};
var protocol = argv.https ? 'https' : 'http',
- runner = new helpers.TestRunner(protocol);
+ target = argv.target ? argv.target : 'http',
+ runner = new helpers.TestRunner(protocol, target);
vows.describe('node-http-proxy/' + protocol).addBatch({
"When using server created by httpProxy.createServer()": {

0 comments on commit 8f28618

Please sign in to comment.
Something went wrong with that request. Please try again.