@@ -518,24 +519,28 @@ 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-65"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-65">¶</a></div><p>Get the Printable data</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-66"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-66">¶</a></div><p>Get the Non-Printable data</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-67"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-67">¶</a></div><p>Replace the host and origin headers in the Printable data</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">remoteHost</span><spanclass="p">,</span><spanclass="nx">options</span><spanclass="p">.</span><spanclass="nx">host</span><spanclass="p">)</span>
<spanclass="k">try</span><spanclass="p">{</span></pre></div></td></tr><trid="section-68"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-68">¶</a></div><p>Write the printable and non-printable data to the socket
<spanclass="k">if</span><spanclass="p">(</span><spanclass="nx">self</span><spanclass="p">.</span><spanclass="nx">https</span><spanclass="o">&&</span><spanclass="o">!</span><spanclass="nx">self</span><spanclass="p">.</span><spanclass="nx">target</span><spanclass="p">.</span><spanclass="nx">https</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>If the proxy server is running HTTPS but the client is running
HTTP then replace <code>ws</code> with <code>wss</code> in the data sent back to the client.</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="s1">'ws:'</span><spanclass="p">,</span><spanclass="s1">'wss:'</span><spanclass="p">);</span>
<spanclass="p">}</span>
<spanclass="k">try</span><spanclass="p">{</span></pre></div></td></tr><trid="section-69"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-69">¶</a></div><p>Write the printable and non-printable data to the socket
from the original incoming request. </p></td><tdclass="code"><divclass="highlight"><pre><spanclass="nx">socket</span><spanclass="p">.</span><spanclass="nx">write</span><spanclass="p">(</span><spanclass="nx">sdata</span><spanclass="p">);</span>
<spanclass="p">}</span></pre></div></td></tr><trid="section-69"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-69">¶</a></div><p>Catch socket errors</p></td><tdclass="code"><divclass="highlight"><pre><spanclass="nx">socket</span><spanclass="p">.</span><spanclass="nx">on</span><spanclass="p">(</span><spanclass="s1">'error'</span><spanclass="p">,</span><spanclass="nx">proxyError</span><spanclass="p">);</span></pre></div></td></tr><trid="section-70"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-70">¶</a></div><p>Remove data listener now that the 'handshake' is complete</p></td><tdclass="code"><divclass="highlight"><pre><spanclass="nx">reverseProxy</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-70"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-70">¶</a></div><p>Catch socket errors</p></td><tdclass="code"><divclass="highlight"><pre><spanclass="nx">socket</span><spanclass="p">.</span><spanclass="nx">on</span><spanclass="p">(</span><spanclass="s1">'error'</span><spanclass="p">,</span><spanclass="nx">proxyError</span><spanclass="p">);</span></pre></div></td></tr><trid="section-71"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-71">¶</a></div><p>Remove data listener now that the 'handshake' is complete</p></td><tdclass="code"><divclass="highlight"><pre><spanclass="nx">reverseProxy</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="k">try</span><spanclass="p">{</span></pre></div></td></tr><trid="section-71"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-71">¶</a></div><p>Attempt to write the upgrade-head to the reverseProxy request.</p></td><tdclass="code"><divclass="highlight"><pre><spanclass="nx">reverseProxy</span><spanclass="p">.</span><spanclass="nx">write</span><spanclass="p">(</span><spanclass="nx">head</span><spanclass="p">);</span>
<spanclass="k">try</span><spanclass="p">{</span></pre></div></td></tr><trid="section-72"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-72">¶</a></div><p>Attempt to write the upgrade-head to the reverseProxy request.</p></td><tdclass="code"><divclass="highlight"><pre><spanclass="nx">reverseProxy</span><spanclass="p">.</span><spanclass="nx">write</span><spanclass="p">(</span><spanclass="nx">head</span><spanclass="p">);</span>
</pre></div></td></tr><trid="section-72"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-72">¶</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">options</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-73"><tdclass="docs"><divclass="pilwrap"><aclass="pilcrow"href="#section-73">¶</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">options</span><spanclass="p">.</span><spanclass="nx">buffer</span><spanclass="o">&&</span><spanclass="o">!</span><spanclass="nx">errState</span><spanclass="p">)</span><spanclass="p">{</span>