<spanclass="nx">winston</span><spanclass="p">.</span><spanclass="nx">verbose</span><spanclass="p">(</span><spanclass="s1">'Incoming HTTP request to: '</span><spanclass="o">+</span><spanclass="nx">req</span><spanclass="p">.</span><spanclass="nx">headers</span><spanclass="p">.</span><spanclass="nx">host</span><spanclass="o">+</span><spanclass="nx">req</span><spanclass="p">.</span><spanclass="nx">url</span><spanclass="p">);</span></pre></div></td></tr><trid="section-8"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-8">¶</a></div><p>If we were passed a callback to process the request
<spanclass="nx">proxy</span><spanclass="p">.</span><spanclass="nx">emit</span><spanclass="p">(</span><spanclass="s1">'request'</span><spanclass="p">,</span><spanclass="nx">req</span><spanclass="p">,</span><spanclass="nx">request</span><spanclass="p">.</span><spanclass="nx">headers</span><spanclass="p">.</span><spanclass="nx">host</span><spanclass="p">,</span><spanclass="nx">req</span><spanclass="p">.</span><spanclass="nx">url</span><spanclass="p">);</span></pre></div></td></tr><trid="section-8"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-8">¶</a></div><p>If we were passed a callback to process the request
or response in some way, then call it.</p></td><tdclass="code"><divclass="highlight"><pre><spanclass="k">if</span><spanclass="p">(</span><spanclass="nx">callback</span><spanclass="p">)</span><spanclass="p">{</span>
or response in some way, then call it.</p></td><tdclass="code"><divclass="highlight"><pre><spanclass="k">if</span><spanclass="p">(</span><spanclass="nx">callback</span><spanclass="p">)</span><spanclass="p">{</span>
<spanclass="nx">winston</span><spanclass="p">.</span><spanclass="nx">verbose</span><spanclass="p">(</span><spanclass="s1">'Proxying HTTP request to: '</span><spanclass="o">+</span><spanclass="nx">host</span><spanclass="o">+</span><spanclass="s1">':'</span><spanclass="o">+</span><spanclass="nx">port</span><spanclass="p">);</span>
<spanclass="nx">winston</span><spanclass="p">.</span><spanclass="nx">verbose</span><spanclass="p">(</span><spanclass="s1">'Proxying request using proxy table'</span><spanclass="p">);</span>
</pre></div></td></tr><trid="section-19"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-19">¶</a></div><p>If forwarding is enabled for this instance, foward proxy the
</pre></div></td></tr><trid="section-19"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-19">¶</a></div><p>Emit the <code>start</code> event indicating that we have begun the proxy operation.</p></td><tdclass="code"><divclass="highlight"><pre><spanclass="k">this</span><spanclass="p">.</span><spanclass="nx">emit</span><spanclass="p">(</span><spanclass="s1">'start'</span><spanclass="p">,</span><spanclass="nx">req</span><spanclass="p">,</span><spanclass="nx">res</span><spanclass="p">,</span><spanclass="nx">host</span><spanclass="p">,</span><spanclass="nx">port</span><spanclass="p">);</span>
</pre></div></td></tr><trid="section-20"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-20">¶</a></div><p>If forwarding is enabled for this instance, foward proxy the
specified request to the address provided in <code>this.options.forward</code></p></td><tdclass="code"><divclass="highlight"><pre><spanclass="k">if</span><spanclass="p">(</span><spanclass="k">this</span><spanclass="p">.</span><spanclass="nx">options</span><spanclass="p">.</span><spanclass="nx">forward</span><spanclass="p">)</span><spanclass="p">{</span>
specified request to the address provided in <code>this.options.forward</code></p></td><tdclass="code"><divclass="highlight"><pre><spanclass="k">if</span><spanclass="p">(</span><spanclass="k">this</span><spanclass="p">.</span><spanclass="nx">options</span><spanclass="p">.</span><spanclass="nx">forward</span><spanclass="p">)</span><spanclass="p">{</span>
<spanclass="nx">winston</span><spanclass="p">.</span><spanclass="nx">verbose</span><spanclass="p">(</span><spanclass="s1">'Forwarding HTTP request to: '</span><spanclass="o">+</span><spanclass="k">this</span><spanclass="p">.</span><spanclass="nx">options</span><spanclass="p">.</span><spanclass="nx">forward</span><spanclass="p">.</span><spanclass="nx">host</span><spanclass="o">+</span><spanclass="s1">':'</span><spanclass="o">+</span><spanclass="k">this</span><spanclass="p">.</span><spanclass="nx">options</span><spanclass="p">.</span><spanclass="nx">forward</span><spanclass="p">.</span><spanclass="nx">port</span><spanclass="p">);</span>
</pre></div></td></tr><trid="section-21"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-21">¶</a></div><p>Force the <code>connection</code> header to be 'close' until
</pre></div></td></tr><trid="section-22"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-22">¶</a></div><p>Force the <code>connection</code> header to be 'close' until
</pre></div></td></tr><trid="section-22"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-22">¶</a></div><p>Open new HTTP request to internal resource with will act as a reverse proxy pass</p></td><tdclass="code"><divclass="highlight"><pre><spanclass="nx">reverseProxy</span><spanclass="o">=</span><spanclass="nx">http</span><spanclass="p">.</span><spanclass="nx">request</span><spanclass="p">(</span><spanclass="nx">opts</span><spanclass="p">,</span><spanclass="kd">function</span><spanclass="p">(</span><spanclass="nx">response</span><spanclass="p">)</span><spanclass="p">{</span>
</pre></div></td></tr><trid="section-23"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-23">¶</a></div><p>Open new HTTP request to internal resource with will act as a reverse proxy pass</p></td><tdclass="code"><divclass="highlight"><pre><spanclass="nx">reverseProxy</span><spanclass="o">=</span><spanclass="nx">http</span><spanclass="p">.</span><spanclass="nx">request</span><spanclass="p">(</span><spanclass="nx">opts</span><spanclass="p">,</span><spanclass="kd">function</span><spanclass="p">(</span><spanclass="nx">response</span><spanclass="p">)</span><spanclass="p">{</span>
</pre></div></td></tr><trid="section-23"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-23">¶</a></div><p>Process the <code>reverseProxy</code><code>response</code> when it's received.</p></td><tdclass="code"><divclass="highlight"><pre><spanclass="k">if</span><spanclass="p">(</span><spanclass="nx">response</span><spanclass="p">.</span><spanclass="nx">headers</span><spanclass="p">.</span><spanclass="nx">connection</span><spanclass="p">)</span><spanclass="p">{</span>
</pre></div></td></tr><trid="section-24"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-24">¶</a></div><p>Process the <code>reverseProxy</code><code>response</code> when it's received.</p></td><tdclass="code"><divclass="highlight"><pre><spanclass="k">if</span><spanclass="p">(</span><spanclass="nx">response</span><spanclass="p">.</span><spanclass="nx">headers</span><spanclass="p">.</span><spanclass="nx">connection</span><spanclass="p">)</span><spanclass="p">{</span>
<spanclass="p">}</span></pre></div></td></tr><trid="section-24"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-24">¶</a></div><p>Set the headers of the client response</p></td><tdclass="code"><divclass="highlight"><pre><spanclass="nx">res</span><spanclass="p">.</span><spanclass="nx">writeHead</span><spanclass="p">(</span><spanclass="nx">response</span><spanclass="p">.</span><spanclass="nx">statusCode</span><spanclass="p">,</span><spanclass="nx">response</span><spanclass="p">.</span><spanclass="nx">headers</span><spanclass="p">);</span></pre></div></td></tr><trid="section-25"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-25">¶</a></div><p><code>response.statusCode === 304</code>: No 'data' event and no 'end'</p></td><tdclass="code"><divclass="highlight"><pre><spanclass="k">if</span><spanclass="p">(</span><spanclass="nx">response</span><spanclass="p">.</span><spanclass="nx">statusCode</span><spanclass="o">===</span><spanclass="mi">304</span><spanclass="p">)</span><spanclass="p">{</span>
<spanclass="p">}</span></pre></div></td></tr><trid="section-25"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-25">¶</a></div><p>Set the headers of the client response</p></td><tdclass="code"><divclass="highlight"><pre><spanclass="nx">res</span><spanclass="p">.</span><spanclass="nx">writeHead</span><spanclass="p">(</span><spanclass="nx">response</span><spanclass="p">.</span><spanclass="nx">statusCode</span><spanclass="p">,</span><spanclass="nx">response</span><spanclass="p">.</span><spanclass="nx">headers</span><spanclass="p">);</span></pre></div></td></tr><trid="section-26"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-26">¶</a></div><p><code>response.statusCode === 304</code>: No 'data' event and no 'end'</p></td><tdclass="code"><divclass="highlight"><pre><spanclass="k">if</span><spanclass="p">(</span><spanclass="nx">response</span><spanclass="p">.</span><spanclass="nx">statusCode</span><spanclass="o">===</span><spanclass="mi">304</span><spanclass="p">)</span><spanclass="p">{</span>
<spanclass="p">}</span></pre></div></td></tr><trid="section-26"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-26">¶</a></div><p>For each data <code>chunk</code> received from the <code>reverseProxy</code>
<spanclass="p">}</span></pre></div></td></tr><trid="section-27"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-27">¶</a></div><p>For each data <code>chunk</code> received from the <code>reverseProxy</code>
<code>response</code> write it to the outgoing <code>res</code>.</p></td><tdclass="code"><divclass="highlight"><pre><spanclass="nx">response</span><spanclass="p">.</span><spanclass="nx">on</span><spanclass="p">(</span><spanclass="s1">'data'</span><spanclass="p">,</span><spanclass="kd">function</span><spanclass="p">(</span><spanclass="nx">chunk</span><spanclass="p">)</span><spanclass="p">{</span>
<code>response</code> write it to the outgoing <code>res</code>.</p></td><tdclass="code"><divclass="highlight"><pre><spanclass="nx">response</span><spanclass="p">.</span><spanclass="nx">on</span><spanclass="p">(</span><spanclass="s1">'data'</span><spanclass="p">,</span><spanclass="kd">function</span><spanclass="p">(</span><spanclass="nx">chunk</span><spanclass="p">)</span><spanclass="p">{</span>
<spanclass="p">});</span></pre></div></td></tr><trid="section-27"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-27">¶</a></div><p>When the <code>reverseProxy</code><code>response</code> ends, end the
<spanclass="p">});</span></pre></div></td></tr><trid="section-28"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-28">¶</a></div><p>When the <code>reverseProxy</code><code>response</code> ends, end the
corresponding outgoing <code>res</code> unless we have entered
corresponding outgoing <code>res</code> unless we have entered
an error state. In which case, assume <code>res.end()</code> has
an error state. In which case, assume <code>res.end()</code> has
already been called and the 'error' event listener
already been called and the 'error' event listener
</pre></div></td></tr><trid="section-28"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-28">¶</a></div><p>Handle 'error' events from the <code>reverseProxy</code>.</p></td><tdclass="code"><divclass="highlight"><pre><spanclass="nx">reverseProxy</span><spanclass="p">.</span><spanclass="nx">once</span><spanclass="p">(</span><spanclass="s1">'error'</span><spanclass="p">,</span><spanclass="nx">proxyError</span><spanclass="p">);</span>
</pre></div></td></tr><trid="section-29"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-29">¶</a></div><p>Handle 'error' events from the <code>reverseProxy</code>.</p></td><tdclass="code"><divclass="highlight"><pre><spanclass="nx">reverseProxy</span><spanclass="p">.</span><spanclass="nx">once</span><spanclass="p">(</span><spanclass="s1">'error'</span><spanclass="p">,</span><spanclass="nx">proxyError</span><spanclass="p">);</span>
</pre></div></td></tr><trid="section-29"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-29">¶</a></div><p>For each data <code>chunk</code> received from the incoming
</pre></div></td></tr><trid="section-30"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-30">¶</a></div><p>For each data <code>chunk</code> received from the incoming
<code>req</code> write it to the <code>reverseProxy</code> request.</p></td><tdclass="code"><divclass="highlight"><pre><spanclass="nx">req</span><spanclass="p">.</span><spanclass="nx">on</span><spanclass="p">(</span><spanclass="s1">'data'</span><spanclass="p">,</span><spanclass="kd">function</span><spanclass="p">(</span><spanclass="nx">chunk</span><spanclass="p">)</span><spanclass="p">{</span>
<code>req</code> write it to the <code>reverseProxy</code> request.</p></td><tdclass="code"><divclass="highlight"><pre><spanclass="nx">req</span><spanclass="p">.</span><spanclass="nx">on</span><spanclass="p">(</span><spanclass="s1">'data'</span><spanclass="p">,</span><spanclass="kd">function</span><spanclass="p">(</span><spanclass="nx">chunk</span><spanclass="p">)</span><spanclass="p">{</span>
<spanclass="p">});</span></pre></div></td></tr><trid="section-30"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-30">¶</a></div><p>When the incoming <code>req</code> ends, end the corresponding <code>reverseProxy</code>
<spanclass="p">});</span></pre></div></td></tr><trid="section-31"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-31">¶</a></div><p>When the incoming <code>req</code> ends, end the corresponding <code>reverseProxy</code>
request unless we have entered an error state. </p></td><tdclass="code"><divclass="highlight"><pre><spanclass="nx">req</span><spanclass="p">.</span><spanclass="nx">on</span><spanclass="p">(</span><spanclass="s1">'end'</span><spanclass="p">,</span><spanclass="kd">function</span><spanclass="p">()</span><spanclass="p">{</span>
request unless we have entered an error state. </p></td><tdclass="code"><divclass="highlight"><pre><spanclass="nx">req</span><spanclass="p">.</span><spanclass="nx">on</span><spanclass="p">(</span><spanclass="s1">'end'</span><spanclass="p">,</span><spanclass="kd">function</span><spanclass="p">()</span><spanclass="p">{</span>
<spanclass="p">});</span></pre></div></td></tr><trid="section-31"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-31">¶</a></div><p>If we have been passed buffered data, resume it.</p></td><tdclass="code"><divclass="highlight"><pre><spanclass="k">if</span><spanclass="p">(</span><spanclass="nx">buffer</span><spanclass="o">&&</span><spanclass="o">!</span><spanclass="nx">errState</span><spanclass="p">)</span><spanclass="p">{</span>
<spanclass="p">});</span></pre></div></td></tr><trid="section-32"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-32">¶</a></div><p>If we have been passed buffered data, resume it.</p></td><tdclass="code"><divclass="highlight"><pre><spanclass="k">if</span><spanclass="p">(</span><spanclass="nx">buffer</span><spanclass="o">&&</span><spanclass="o">!</span><spanclass="nx">errState</span><spanclass="p">)</span><spanclass="p">{</span>
</pre></div></td></tr><trid="section-32"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-32">¶</a></div><h3>@private function _forwardRequest (req)</h3>
</pre></div></td></tr><trid="section-33"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-33">¶</a></div><h3>@private function _forwardRequest (req)</h3>
<h4>@req {ServerRequest} Incoming HTTP Request to proxy.</h4>
<h4>@req {ServerRequest} Incoming HTTP Request to proxy.</h4>
...
@@ -320,31 +318,31 @@ by <code>this.options.forward</code> ignoring errors and the subsequent response
...
@@ -320,31 +318,31 @@ by <code>this.options.forward</code> ignoring errors and the subsequent response
</pre></div></td></tr><trid="section-33"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-33">¶</a></div><p>Force the <code>connection</code> header to be 'close' until
</pre></div></td></tr><trid="section-34"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-34">¶</a></div><p>Force the <code>connection</code> header to be 'close' until
</pre></div></td></tr><trid="section-34"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-34">¶</a></div><p>Open new HTTP request to internal resource with will act as a reverse proxy pass</p></td><tdclass="code"><divclass="highlight"><pre><spanclass="nx">forwardProxy</span><spanclass="o">=</span><spanclass="nx">http</span><spanclass="p">.</span><spanclass="nx">request</span><spanclass="p">(</span><spanclass="nx">opts</span><spanclass="p">,</span><spanclass="kd">function</span><spanclass="p">(</span><spanclass="nx">response</span><spanclass="p">)</span><spanclass="p">{</span></pre></div></td></tr><trid="section-35"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-35">¶</a></div><p>Ignore the response from the forward proxy since this is a 'fire-and-forget' proxy.
</pre></div></td></tr><trid="section-35"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-35">¶</a></div><p>Open new HTTP request to internal resource with will act as a reverse proxy pass</p></td><tdclass="code"><divclass="highlight"><pre><spanclass="nx">forwardProxy</span><spanclass="o">=</span><spanclass="nx">http</span><spanclass="p">.</span><spanclass="nx">request</span><spanclass="p">(</span><spanclass="nx">opts</span><spanclass="p">,</span><spanclass="kd">function</span><spanclass="p">(</span><spanclass="nx">response</span><spanclass="p">)</span><spanclass="p">{</span></pre></div></td></tr><trid="section-36"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-36">¶</a></div><p>Ignore the response from the forward proxy since this is a 'fire-and-forget' proxy.
Remark (indexzero): We will eventually emit a 'forward' event here for performance tuning.</p></td><tdclass="code"><divclass="highlight"><pre><spanclass="p">});</span>
Remark (indexzero): We will eventually emit a 'forward' event here for performance tuning.</p></td><tdclass="code"><divclass="highlight"><pre><spanclass="p">});</span>
</pre></div></td></tr><trid="section-36"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-36">¶</a></div><p>Add a listener for the connection timeout event.</p>
</pre></div></td></tr><trid="section-37"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-37">¶</a></div><p>Add a listener for the connection timeout event.</p>
<p>Remark: Ignoring this error in the event
<p>Remark: Ignoring this error in the event
forward target doesn't exist.</p></td><tdclass="code"><divclass="highlight"><pre><spanclass="nx">forwardProxy</span><spanclass="p">.</span><spanclass="nx">once</span><spanclass="p">(</span><spanclass="s1">'error'</span><spanclass="p">,</span><spanclass="kd">function</span><spanclass="p">(</span><spanclass="nx">err</span><spanclass="p">)</span><spanclass="p">{</span><spanclass="p">});</span></pre></div></td></tr><trid="section-37"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-37">¶</a></div><p>Chunk the client request body as chunks from the proxied request come in</p></td><tdclass="code"><divclass="highlight"><pre><spanclass="nx">req</span><spanclass="p">.</span><spanclass="nx">on</span><spanclass="p">(</span><spanclass="s1">'data'</span><spanclass="p">,</span><spanclass="kd">function</span><spanclass="p">(</span><spanclass="nx">chunk</span><spanclass="p">)</span><spanclass="p">{</span>
forward target doesn't exist.</p></td><tdclass="code"><divclass="highlight"><pre><spanclass="nx">forwardProxy</span><spanclass="p">.</span><spanclass="nx">once</span><spanclass="p">(</span><spanclass="s1">'error'</span><spanclass="p">,</span><spanclass="kd">function</span><spanclass="p">(</span><spanclass="nx">err</span><spanclass="p">)</span><spanclass="p">{</span><spanclass="p">});</span></pre></div></td></tr><trid="section-38"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-38">¶</a></div><p>Chunk the client request body as chunks from the proxied request come in</p></td><tdclass="code"><divclass="highlight"><pre><spanclass="nx">req</span><spanclass="p">.</span><spanclass="nx">on</span><spanclass="p">(</span><spanclass="s1">'data'</span><spanclass="p">,</span><spanclass="kd">function</span><spanclass="p">(</span><spanclass="nx">chunk</span><spanclass="p">)</span><spanclass="p">{</span>
<spanclass="p">})</span></pre></div></td></tr><trid="section-38"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-38">¶</a></div><p>At the end of the client request, we are going to stop the proxied request</p></td><tdclass="code"><divclass="highlight"><pre><spanclass="nx">req</span><spanclass="p">.</span><spanclass="nx">on</span><spanclass="p">(</span><spanclass="s1">'end'</span><spanclass="p">,</span><spanclass="kd">function</span><spanclass="p">()</span><spanclass="p">{</span>
<spanclass="p">})</span></pre></div></td></tr><trid="section-39"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-39">¶</a></div><p>At the end of the client request, we are going to stop the proxied request</p></td><tdclass="code"><divclass="highlight"><pre><spanclass="nx">req</span><spanclass="p">.</span><spanclass="nx">on</span><spanclass="p">(</span><spanclass="s1">'end'</span><spanclass="p">,</span><spanclass="kd">function</span><spanclass="p">()</span><spanclass="p">{</span>
<spanclass="nx">headers</span><spanclass="o">=</span><spanclass="k">new</span><spanclass="nx">_headers</span><spanclass="p">(</span><spanclass="nx">req</span><spanclass="p">.</span><spanclass="nx">headers</span><spanclass="p">),</span><spanclass="nx">CRLF</span><spanclass="o">=</span><spanclass="s1">'\r\n'</span><spanclass="p">;</span></pre></div></td></tr><trid="section-39"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-39">¶</a></div><p>Will generate clone of headers
<spanclass="nx">headers</span><spanclass="o">=</span><spanclass="k">new</span><spanclass="nx">_headers</span><spanclass="p">(</span><spanclass="nx">req</span><spanclass="p">.</span><spanclass="nx">headers</span><spanclass="p">),</span><spanclass="nx">CRLF</span><spanclass="o">=</span><spanclass="s1">'\r\n'</span><spanclass="p">;</span></pre></div></td></tr><trid="section-40"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-40">¶</a></div><p>Will generate clone of headers
To not change original</p></td><tdclass="code"><divclass="highlight"><pre><spanclass="kd">function</span><spanclass="nx">_headers</span><spanclass="p">(</span><spanclass="nx">headers</span><spanclass="p">)</span><spanclass="p">{</span>
To not change original</p></td><tdclass="code"><divclass="highlight"><pre><spanclass="kd">function</span><spanclass="nx">_headers</span><spanclass="p">(</span><spanclass="nx">headers</span><spanclass="p">)</span><spanclass="p">{</span>
<spanclass="p">}</span></pre></div></td></tr><trid="section-40"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-40">¶</a></div><p>WebSocket requests has method = GET</p></td><tdclass="code"><divclass="highlight"><pre><spanclass="k">if</span><spanclass="p">(</span><spanclass="nx">req</span><spanclass="p">.</span><spanclass="nx">method</span><spanclass="o">!==</span><spanclass="s1">'GET'</span><spanclass="o">||</span><spanclass="nx">headers</span><spanclass="p">.</span><spanclass="nx">upgrade</span><spanclass="p">.</span><spanclass="nx">toLowerCase</span><spanclass="p">()</span><spanclass="o">!==</span><spanclass="s1">'websocket'</span><spanclass="p">)</span><spanclass="p">{</span></pre></div></td></tr><trid="section-41"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-41">¶</a></div><p>This request is not WebSocket request</p></td><tdclass="code"><divclass="highlight"><pre><spanclass="k">return</span><spanclass="p">;</span>
<spanclass="p">}</span></pre></div></td></tr><trid="section-41"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-41">¶</a></div><p>WebSocket requests has method = GET</p></td><tdclass="code"><divclass="highlight"><pre><spanclass="k">if</span><spanclass="p">(</span><spanclass="nx">req</span><spanclass="p">.</span><spanclass="nx">method</span><spanclass="o">!==</span><spanclass="s1">'GET'</span><spanclass="o">||</span><spanclass="nx">headers</span><spanclass="p">.</span><spanclass="nx">upgrade</span><spanclass="p">.</span><spanclass="nx">toLowerCase</span><spanclass="p">()</span><spanclass="o">!==</span><spanclass="s1">'websocket'</span><spanclass="p">)</span><spanclass="p">{</span></pre></div></td></tr><trid="section-42"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-42">¶</a></div><p>This request is not WebSocket request</p></td><tdclass="code"><divclass="highlight"><pre><spanclass="k">return</span><spanclass="p">;</span>
<spanclass="p">}</span></pre></div></td></tr><trid="section-42"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-42">¶</a></div><p>Turn of all bufferings
<spanclass="p">}</span></pre></div></td></tr><trid="section-43"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-43">¶</a></div><p>Turn of all bufferings
For server set KeepAlive
For server set KeepAlive
For client set encoding</p></td><tdclass="code"><divclass="highlight"><pre><spanclass="kd">function</span><spanclass="nx">_socket</span><spanclass="p">(</span><spanclass="nx">socket</span><spanclass="p">,</span><spanclass="nx">server</span><spanclass="p">)</span><spanclass="p">{</span>
For client set encoding</p></td><tdclass="code"><divclass="highlight"><pre><spanclass="kd">function</span><spanclass="nx">_socket</span><spanclass="p">(</span><spanclass="nx">socket</span><spanclass="p">,</span><spanclass="nx">server</span><spanclass="p">)</span><spanclass="p">{</span>
<spanclass="p">}</span></pre></div></td></tr><trid="section-43"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-43">¶</a></div><p>Client socket</p></td><tdclass="code"><divclass="highlight"><pre><spanclass="nx">_socket</span><spanclass="p">(</span><spanclass="nx">socket</span><spanclass="p">);</span></pre></div></td></tr><trid="section-44"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-44">¶</a></div><p>If host is undefined
<spanclass="p">}</span></pre></div></td></tr><trid="section-44"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-44">¶</a></div><p>Client socket</p></td><tdclass="code"><divclass="highlight"><pre><spanclass="nx">_socket</span><spanclass="p">(</span><spanclass="nx">socket</span><spanclass="p">);</span></pre></div></td></tr><trid="section-45"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-45">¶</a></div><p>If host is undefined
Get it from headers</p></td><tdclass="code"><divclass="highlight"><pre><spanclass="k">if</span><spanclass="p">(</span><spanclass="o">!</span><spanclass="nx">host</span><spanclass="p">)</span><spanclass="p">{</span>
Get it from headers</p></td><tdclass="code"><divclass="highlight"><pre><spanclass="k">if</span><spanclass="p">(</span><spanclass="o">!</span><spanclass="nx">host</span><spanclass="p">)</span><spanclass="p">{</span>
<spanclass="nx">p</span><spanclass="p">.</span><spanclass="nx">getClient</span><spanclass="p">(</span><spanclass="kd">function</span><spanclass="p">(</span><spanclass="nx">client</span><spanclass="p">)</span><spanclass="p">{</span></pre></div></td></tr><trid="section-48"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-48">¶</a></div><p>Based on 'pool/main.js'</p></td><tdclass="code"><divclass="highlight"><pre><spanclass="kd">var</span><spanclass="nx">request</span><spanclass="o">=</span><spanclass="nx">client</span><spanclass="p">.</span><spanclass="nx">request</span><spanclass="p">(</span><spanclass="s1">'GET'</span><spanclass="p">,</span><spanclass="nx">req</span><spanclass="p">.</span><spanclass="nx">url</span><spanclass="p">,</span><spanclass="nx">headers</span><spanclass="p">);</span>
<spanclass="nx">p</span><spanclass="p">.</span><spanclass="nx">getClient</span><spanclass="p">(</span><spanclass="kd">function</span><spanclass="p">(</span><spanclass="nx">client</span><spanclass="p">)</span><spanclass="p">{</span></pre></div></td></tr><trid="section-49"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-49">¶</a></div><p>Based on 'pool/main.js'</p></td><tdclass="code"><divclass="highlight"><pre><spanclass="kd">var</span><spanclass="nx">request</span><spanclass="o">=</span><spanclass="nx">client</span><spanclass="p">.</span><spanclass="nx">request</span><spanclass="p">(</span><spanclass="s1">'GET'</span><spanclass="p">,</span><spanclass="nx">req</span><spanclass="p">.</span><spanclass="nx">url</span><spanclass="p">,</span><spanclass="nx">headers</span><spanclass="p">);</span>
</pre></div></td></tr><trid="section-49"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-49">¶</a></div><p>Remove the client from the pool's available clients since it has errored</p></td><tdclass="code"><divclass="highlight"><pre><spanclass="nx">p</span><spanclass="p">.</span><spanclass="nx">clients</span><spanclass="p">.</span><spanclass="nx">splice</span><spanclass="p">(</span><spanclass="nx">p</span><spanclass="p">.</span><spanclass="nx">clients</span><spanclass="p">.</span><spanclass="nx">indexOf</span><spanclass="p">(</span><spanclass="nx">client</span><spanclass="p">),</span><spanclass="mi">1</span><spanclass="p">);</span>
</pre></div></td></tr><trid="section-50"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-50">¶</a></div><p>Remove the client from the pool's available clients since it has errored</p></td><tdclass="code"><divclass="highlight"><pre><spanclass="nx">p</span><spanclass="p">.</span><spanclass="nx">clients</span><spanclass="p">.</span><spanclass="nx">splice</span><spanclass="p">(</span><spanclass="nx">p</span><spanclass="p">.</span><spanclass="nx">clients</span><spanclass="p">.</span><spanclass="nx">indexOf</span><spanclass="p">(</span><spanclass="nx">client</span><spanclass="p">),</span><spanclass="mi">1</span><spanclass="p">);</span>
<spanclass="nx">request</span><spanclass="p">.</span><spanclass="nx">socket</span><spanclass="p">.</span><spanclass="nx">on</span><spanclass="p">(</span><spanclass="s1">'data'</span><spanclass="p">,</span><spanclass="nx">handshake</span><spanclass="o">=</span><spanclass="kd">function</span><spanclass="p">(</span><spanclass="nx">data</span><spanclass="p">)</span><spanclass="p">{</span></pre></div></td></tr><trid="section-53"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-53">¶</a></div><p>Handshaking</p></td><tdclass="code"><divclass="highlight"><pre></pre></div></td></tr><trid="section-54"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-54">¶</a></div><p>Ok, kind of harmfull part of code
<spanclass="nx">request</span><spanclass="p">.</span><spanclass="nx">socket</span><spanclass="p">.</span><spanclass="nx">on</span><spanclass="p">(</span><spanclass="s1">'data'</span><spanclass="p">,</span><spanclass="nx">handshake</span><spanclass="o">=</span><spanclass="kd">function</span><spanclass="p">(</span><spanclass="nx">data</span><spanclass="p">)</span><spanclass="p">{</span></pre></div></td></tr><trid="section-54"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-54">¶</a></div><p>Handshaking</p></td><tdclass="code"><divclass="highlight"><pre></pre></div></td></tr><trid="section-55"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-55">¶</a></div><p>Ok, kind of harmfull part of code
Socket.IO is sending hash at the end of handshake
Socket.IO is sending hash at the end of handshake
If protocol = 76
If protocol = 76
But we need to replace 'host' and 'origin' in response
But we need to replace 'host' and 'origin' in response
So we split data to printable data and to non-printable
So we split data to printable data and to non-printable
(Non-printable will come after double-CRLF)</p></td><tdclass="code"><divclass="highlight"><pre><spanclass="kd">var</span><spanclass="nx">sdata</span><spanclass="o">=</span><spanclass="nx">data</span><spanclass="p">.</span><spanclass="nx">toString</span><spanclass="p">();</span></pre></div></td></tr><trid="section-55"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-55">¶</a></div><p>Get Printable</p></td><tdclass="code"><divclass="highlight"><pre><spanclass="nx">sdata</span><spanclass="o">=</span><spanclass="nx">sdata</span><spanclass="p">.</span><spanclass="nx">substr</span><spanclass="p">(</span><spanclass="mi">0</span><spanclass="p">,</span><spanclass="nx">sdata</span><spanclass="p">.</span><spanclass="nx">search</span><spanclass="p">(</span><spanclass="nx">CRLF</span><spanclass="o">+</span><spanclass="nx">CRLF</span><spanclass="p">));</span></pre></div></td></tr><trid="section-56"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-56">¶</a></div><p>Get Non-Printable</p></td><tdclass="code"><divclass="highlight"><pre><spanclass="nx">data</span><spanclass="o">=</span><spanclass="nx">data</span><spanclass="p">.</span><spanclass="nx">slice</span><spanclass="p">(</span><spanclass="nx">Buffer</span><spanclass="p">.</span><spanclass="nx">byteLength</span><spanclass="p">(</span><spanclass="nx">sdata</span><spanclass="p">),</span><spanclass="nx">data</span><spanclass="p">.</span><spanclass="nx">length</span><spanclass="p">);</span></pre></div></td></tr><trid="section-57"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-57">¶</a></div><p>Replace host and origin</p></td><tdclass="code"><divclass="highlight"><pre><spanclass="nx">sdata</span><spanclass="o">=</span><spanclass="nx">sdata</span><spanclass="p">.</span><spanclass="nx">replace</span><spanclass="p">(</span><spanclass="nx">remote_host</span><spanclass="p">,</span><spanclass="nx">host</span><spanclass="p">)</span>
(Non-printable will come after double-CRLF)</p></td><tdclass="code"><divclass="highlight"><pre><spanclass="kd">var</span><spanclass="nx">sdata</span><spanclass="o">=</span><spanclass="nx">data</span><spanclass="p">.</span><spanclass="nx">toString</span><spanclass="p">();</span></pre></div></td></tr><trid="section-56"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-56">¶</a></div><p>Get Printable</p></td><tdclass="code"><divclass="highlight"><pre><spanclass="nx">sdata</span><spanclass="o">=</span><spanclass="nx">sdata</span><spanclass="p">.</span><spanclass="nx">substr</span><spanclass="p">(</span><spanclass="mi">0</span><spanclass="p">,</span><spanclass="nx">sdata</span><spanclass="p">.</span><spanclass="nx">search</span><spanclass="p">(</span><spanclass="nx">CRLF</span><spanclass="o">+</span><spanclass="nx">CRLF</span><spanclass="p">));</span></pre></div></td></tr><trid="section-57"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-57">¶</a></div><p>Get Non-Printable</p></td><tdclass="code"><divclass="highlight"><pre><spanclass="nx">data</span><spanclass="o">=</span><spanclass="nx">data</span><spanclass="p">.</span><spanclass="nx">slice</span><spanclass="p">(</span><spanclass="nx">Buffer</span><spanclass="p">.</span><spanclass="nx">byteLength</span><spanclass="p">(</span><spanclass="nx">sdata</span><spanclass="p">),</span><spanclass="nx">data</span><spanclass="p">.</span><spanclass="nx">length</span><spanclass="p">);</span></pre></div></td></tr><trid="section-58"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-58">¶</a></div><p>Replace host and origin</p></td><tdclass="code"><divclass="highlight"><pre><spanclass="nx">sdata</span><spanclass="o">=</span><spanclass="nx">sdata</span><spanclass="p">.</span><spanclass="nx">replace</span><spanclass="p">(</span><spanclass="nx">remote_host</span><spanclass="p">,</span><spanclass="nx">host</span><spanclass="p">)</span>
<spanclass="p">});</span></pre></div></td></tr><trid="section-61"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-61">¶</a></div><p>Remove data listener now that the 'handshake' is complete</p></td><tdclass="code"><divclass="highlight"><pre><spanclass="nx">request</span><spanclass="p">.</span><spanclass="nx">socket</span><spanclass="p">.</span><spanclass="nx">removeListener</span><spanclass="p">(</span><spanclass="s1">'data'</span><spanclass="p">,</span><spanclass="nx">handshake</span><spanclass="p">);</span>
<spanclass="p">});</span></pre></div></td></tr><trid="section-62"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-62">¶</a></div><p>Remove data listener now that the 'handshake' is complete</p></td><tdclass="code"><divclass="highlight"><pre><spanclass="nx">request</span><spanclass="p">.</span><spanclass="nx">socket</span><spanclass="p">.</span><spanclass="nx">removeListener</span><spanclass="p">(</span><spanclass="s1">'data'</span><spanclass="p">,</span><spanclass="nx">handshake</span><spanclass="p">);</span>
</pre></div></td></tr><trid="section-64"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-64">¶</a></div><p>We're now connected to the server, so lets change server socket</p></td><tdclass="code"><divclass="highlight"><pre><spanclass="nx">reverse_proxy</span><spanclass="p">.</span><spanclass="nx">on</span><spanclass="p">(</span><spanclass="s1">'data'</span><spanclass="p">,</span><spanclass="nx">listeners</span><spanclass="p">.</span><spanclass="nx">_r_data</span><spanclass="o">=</span><spanclass="kd">function</span><spanclass="p">(</span><spanclass="nx">data</span><spanclass="p">)</span><spanclass="p">{</span></pre></div></td></tr><trid="section-65"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-65">¶</a></div><p>Pass data to client</p></td><tdclass="code"><divclass="highlight"><pre><spanclass="k">if</span><spanclass="p">(</span><spanclass="nx">socket</span><spanclass="p">.</span><spanclass="nx">writable</span><spanclass="p">)</span><spanclass="p">{</span>
</pre></div></td></tr><trid="section-65"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-65">¶</a></div><p>We're now connected to the server, so lets change server socket</p></td><tdclass="code"><divclass="highlight"><pre><spanclass="nx">reverse_proxy</span><spanclass="p">.</span><spanclass="nx">on</span><spanclass="p">(</span><spanclass="s1">'data'</span><spanclass="p">,</span><spanclass="nx">listeners</span><spanclass="p">.</span><spanclass="nx">_r_data</span><spanclass="o">=</span><spanclass="kd">function</span><spanclass="p">(</span><spanclass="nx">data</span><spanclass="p">)</span><spanclass="p">{</span></pre></div></td></tr><trid="section-66"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-66">¶</a></div><p>Pass data to client</p></td><tdclass="code"><divclass="highlight"><pre><spanclass="k">if</span><spanclass="p">(</span><spanclass="nx">socket</span><spanclass="p">.</span><spanclass="nx">writable</span><spanclass="p">)</span><spanclass="p">{</span>
@@ -419,19 +417,19 @@ So we split data to printable data and to non-printable
...
@@ -419,19 +417,19 @@ So we split data to printable data and to non-printable
<spanclass="p">}</span>
<spanclass="p">}</span>
<spanclass="p">});</span>
<spanclass="p">});</span>
<spanclass="nx">socket</span><spanclass="p">.</span><spanclass="nx">on</span><spanclass="p">(</span><spanclass="s1">'data'</span><spanclass="p">,</span><spanclass="nx">listeners</span><spanclass="p">.</span><spanclass="nx">_data</span><spanclass="o">=</span><spanclass="kd">function</span><spanclass="p">(</span><spanclass="nx">data</span><spanclass="p">)</span><spanclass="p">{</span></pre></div></td></tr><trid="section-66"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-66">¶</a></div><p>Pass data from client to server</p></td><tdclass="code"><divclass="highlight"><pre><spanclass="k">try</span><spanclass="p">{</span>
<spanclass="nx">socket</span><spanclass="p">.</span><spanclass="nx">on</span><spanclass="p">(</span><spanclass="s1">'data'</span><spanclass="p">,</span><spanclass="nx">listeners</span><spanclass="p">.</span><spanclass="nx">_data</span><spanclass="o">=</span><spanclass="kd">function</span><spanclass="p">(</span><spanclass="nx">data</span><spanclass="p">)</span><spanclass="p">{</span></pre></div></td></tr><trid="section-67"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-67">¶</a></div><p>Pass data from client to server</p></td><tdclass="code"><divclass="highlight"><pre><spanclass="k">try</span><spanclass="p">{</span>
<spanclass="p">});</span></pre></div></td></tr><trid="section-67"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-67">¶</a></div><p>Detach event listeners from reverse_proxy</p></td><tdclass="code"><divclass="highlight"><pre><spanclass="kd">function</span><spanclass="nx">detach</span><spanclass="p">()</span><spanclass="p">{</span>
<spanclass="p">});</span></pre></div></td></tr><trid="section-68"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-68">¶</a></div><p>Detach event listeners from reverse_proxy</p></td><tdclass="code"><divclass="highlight"><pre><spanclass="kd">function</span><spanclass="nx">detach</span><spanclass="p">()</span><spanclass="p">{</span>