# Proxy Server troubles

**URL:** <https://rubytalk.org/t/proxy-server-troubles/17811>\
**Category:** ruby-talk\
**Created:** [22 April 2005 00:54 UTC](https://rubytalk.org/t/proxy-server-troubles/17811 "2005-04-22T00:54:20Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tanner\_Burson](https://avatars.discourse-cdn.com/v4/letter/t/4bbf92/32.png) [@Tanner\_Burson](https://rubytalk.org/u/Tanner_Burson)\
**Post date:** [22 April 2005 00:54 UTC](https://rubytalk.org/t/proxy-server-troubles/17811/1 "2005-04-22T00:54:20Z")

</div>

I've been working on a proxy server implementation for a project idea  
I have. But I've found that it only seems to work on certain pages,  
and not on others. I've distilled the problem down to a simple  
implementation, but it still occurs. The code below will return a  
(mostly) correct website if pointed at '[www.msn.com](http://www.msn.com)' but if pointed at  
say '[slashdot.org](http://slashdot.org)' the browser refuses to display it, in both cases  
the page is loaded correctly. If anyone has any ideas I'd really  
appreciate it.

===proxy.rb

require 'socket'  
require 'net/http'  
local\_host = '192.168.0.6'  
local\_port = 8080

host = '[slashdot.org](http://slashdot.org)'  
server = TCPServer.new(local\_host,local\_port)  
while true  
&nbsp;&nbsp;Thread.start(server.accept) do |s|   
&nbsp;&nbsp;&nbsp;&nbsp;data = ''  
&nbsp;&nbsp;&nbsp;&nbsp;while s.gets && !$\_.nil?  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data += $\_  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break if $\_ == "\r\n" || $\_ == "\n"  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;p "====Received: #{data}"  
&nbsp;&nbsp;&nbsp;&nbsp;net = Net::HTTP.new(host,80)  
&nbsp;&nbsp;&nbsp;&nbsp;response = net.get('/')  
&nbsp;&nbsp;&nbsp;&nbsp;s \<\< "HTTP/#{response.http\_version}  
#{response.code}/#{response.message} \r\n"  
&nbsp;&nbsp;&nbsp;&nbsp;p "====Got back"  
&nbsp;&nbsp;&nbsp;&nbsp;response.each\_header do |key,value|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tmp = key + ": " + value + "\r\n"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p tmp  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s \<\< tmp  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;p response.body  
&nbsp;&nbsp;&nbsp;&nbsp;s \<\< "\r\n" \<\< response.body  
&nbsp;&nbsp;&nbsp;&nbsp;s.close # We're done with this request, bring on the next.  
&nbsp;&nbsp;end  
end

====end

> **···**
>
> --  
> ===Tanner Burson===  
> [tanner.burson@gmail.com](mailto:tanner.burson@gmail.com)  
> [http://tannerburson.com](http://tannerburson.com) \<---Might even work one day...

---

<div class="post-metadata">

**Author:** ![jm11](https://avatars.discourse-cdn.com/v4/letter/j/b19c9b/32.png) [@jm11](https://rubytalk.org/u/jm11)\
**Post date:** [22 April 2005 01:30 UTC](https://rubytalk.org/t/proxy-server-troubles/17811/2 "2005-04-22T01:30:52Z")

</div>

I gave it a quick run. I had to change  
&nbsp;&nbsp;local\_host = nil  
so that it would bind and fixed the line wrap in the posted code. Telnetted to port 8080 on localhost and hit enter twice and it came back with a lot of html from slashdot. So it works on

$ ruby -v  
ruby 1.8.1 (2003-12-25) [powerpc-darwin]

Can't see anything obviously wrong. Do you have a transparent proxy or anything between you and the site as this can cause havoc?

J.

> **···**
>
> On 22/04/2005, at 10:54 AM, Tanner Burson wrote:
> 
> > I've been working on a proxy server implementation for a project idea  
> > I have. But I've found that it only seems to work on certain pages,  
> > and not on others. I've distilled the problem down to a simple  
> > implementation, but it still occurs. The code below will return a  
> > (mostly) correct website if pointed at 'www.msn.com' but if pointed at  
> > say 'slashdot.org' the browser refuses to display it, in both cases  
> > the page is loaded correctly. If anyone has any ideas I'd really  
> > appreciate it.
> > 
> > ===proxy.rb
> > 
> > require 'socket'  
> > require 'net/http'  
> > local\_host = '192.168.0.6'  
> > local\_port = 8080

---

<div class="post-metadata">

**Author:** ![Ernest\_Ellingson3](https://avatars.discourse-cdn.com/v4/letter/e/58f4c7/32.png) [@Ernest\_Ellingson3](https://rubytalk.org/u/Ernest_Ellingson3)\
**Post date:** [22 April 2005 03:40 UTC](https://rubytalk.org/t/proxy-server-troubles/17811/3 "2005-04-22T03:40:53Z")

</div>

Tanner Burson wrote:

> I've been working on a proxy server implementation for a project idea  
> I have. But I've found that it only seems to work on certain pages,  
> and not on others. I've distilled the problem down to a simple  
> implementation, but it still occurs. The code below will return a  
> (mostly) correct website if pointed at 'www.msn.com' but if pointed at  
> say 'slashdot.org' the browser refuses to display it, in both cases  
> the page is loaded correctly. If anyone has any ideas I'd really  
> appreciate it.
> 
> ===proxy.rb

Take a look at socketproxy.rb at

[http://dev.ctor.org/download/](http://dev.ctor.org/download/)

---

<div class="post-metadata">

**Author:** ![Craig\_Moran](https://avatars.discourse-cdn.com/v4/letter/c/90db22/32.png) [@Craig\_Moran](https://rubytalk.org/u/Craig_Moran)\
**Post date:** [25 April 2005 13:05 UTC](https://rubytalk.org/t/proxy-server-troubles/17811/4 "2005-04-25T13:05:49Z")

</div>

I am behind a proxy server at work, yet got this to work. I commented  
out your Net::HTTP.new line and added what I believe to be the Proxy  
equivalent. Then I set Mozilla Firefox's proxy to be localhost:8080.  
NOTE: I do not have the ability to use another machine to test as I  
am very restricted with net access here. I launched slashdot.org and  
monitored the proxy.rb window while all of the HTML flew by. It came  
up and rendered properly in the browser.

I am on Windows 2000  
C:\\>ruby -v  
ruby 1.8.2 (2004-12-25) [i386-mswin32]

Here's the code. Hope it helps:

require 'socket'  
require 'net/http'  
local\_host = 'localhost'  
local\_port = 8080

host = 'slashdot.org'  
server = TCPServer.new(local\_host, local\_port)  
while true  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Thread.start(server.accept) do |s|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data = ''  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while s.gets && !$\_.nil?  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data += $\_  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break if $\_ == "\r\n" || $\_ == "\n"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p "====Received: #{data}"  
# net = Net::HTTP.new(host,80)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;net = Net::HTTP::Proxy("proxy", "80").start(host)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;response = net.get('/')  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs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p;p response.body  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s \<\< "\r\n" \<\< response.body  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s.close # We're done with this request, bring on the next.  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
end

> **···**
>
> On 4/21/05, Tanner Burson \<tanner.burson@gmail.com\> wrote:
> 
> > I've been working on a proxy server implementation for a project idea  
> > I have. But I've found that it only seems to work on certain pages,  
> > and not on others. I've distilled the problem down to a simple  
> > implementation, but it still occurs. The code below will return a  
> > (mostly) correct website if pointed at 'www.msn.com' but if pointed at  
> > say 'slashdot.org' the browser refuses to display it, in both cases  
> > the page is loaded correctly. If anyone has any ideas I'd really  
> > appreciate it.
> > 
> > ===proxy.rb
> > 
> > require 'socket'  
> > require 'net/http'  
> > local\_host = '192.168.0.6'  
> > local\_port = 8080
> > 
> > host = 'slashdot.org'  
> > server = TCPServer.new(local\_host,local\_port)  
> > while true  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Thread.start(server.accept) do |s|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data = ''  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while s.gets && !$\_.nil?  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data += $\_  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break if $\_ == "\r\n" || $\_ == "\n"  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p "====Received: #{data}"  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;net = Net::HTTP.new(host,80)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;response = net.get('/')  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s \<\< "HTTP/#{response.http\_version}  
> > #{response.code}/#{response.message} \r\n"  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p "====Got back"  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;response.each\_header do |key,value|  
> > &nbsp;&nbsp;&nbs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# We're done with this request, bring on the next.  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > end
> > 
> > ====end  
> > --  
> > ===Tanner Burson===  
> > tanner.burson@gmail.com  
> > [http://tannerburson.com](http://tannerburson.com) \<---Might even work one day...

---

<div class="post-metadata">

**Author:** ![James\_Britt4](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@James\_Britt4](https://rubytalk.org/u/James_Britt4)\
**Post date:** [22 April 2005 01:48 UTC](https://rubytalk.org/t/proxy-server-troubles/17811/5 "2005-04-22T01:48:53Z")

</div>

jm wrote:

> I gave it a quick run. I had to change  
> local\_host = nil  
> so that it would bind and fixed the line wrap in the posted code. Telnetted to port 8080 on localhost and hit enter twice and it came back with a lot of html from slashdot.

I managed the same, on win32, but could not get the results to render in Firefox or IE. It fetches the page OK (judging by the debugging p calls), but whatever is sent back to the browser is off in some way.

James

---

<div class="post-metadata">

**Author:** ![Tanner\_Burson](https://avatars.discourse-cdn.com/v4/letter/t/4bbf92/32.png) [@Tanner\_Burson](https://rubytalk.org/u/Tanner_Burson)\
**Post date:** [22 April 2005 01:49 UTC](https://rubytalk.org/t/proxy-server-troubles/17811/6 "2005-04-22T01:49:35Z")

</div>

Not that I'm aware of. I should have been more specific the first  
time, try setting it as a proxy in your browser settings, I've yet to  
get it to load correctly in a browser.

> **···**
>
> On 4/21/05, jm \<jeffm@ghostgun.com\> wrote:
> 
> > I gave it a quick run. I had to change  
> > &nbsp;&nbsp;local\_host = nil  
> > so that it would bind and fixed the line wrap in the posted code.  
> > Telnetted to port 8080 on localhost and hit enter twice and it came  
> > back with a lot of html from slashdot. So it works on
> > 
> > $ ruby -v  
> > ruby 1.8.1 (2003-12-25) [powerpc-darwin]
> > 
> > Can't see anything obviously wrong. Do you have a transparent proxy or  
> > anything between you and the site as this can cause havoc?
> 
> --  
> ===Tanner Burson===  
> tanner.burson@gmail.com  
> [http://tannerburson.com](http://tannerburson.com) \<---Might even work one day...

---

<div class="post-metadata">

**Author:** ![Craig\_Moran](https://avatars.discourse-cdn.com/v4/letter/c/90db22/32.png) [@Craig\_Moran](https://rubytalk.org/u/Craig_Moran)\
**Post date:** [25 April 2005 13:39 UTC](https://rubytalk.org/t/proxy-server-troubles/17811/7 "2005-04-25T13:39:12Z")

</div>

I neglected to mention that if you use the proxy version of Net::HTTP,  
you can get it to work without a proxy by putting in "" for the proxy  
and port. Here's the proxy version:

net = Net::HTTP::Proxy("proxy", "80").start(host)

and the non-proxy version:

net = Net::HTTP::Proxy("", "").start(host)

HTH,  
Craig

> **···**
>
> On 4/25/05, Craig Moran \<craig.m.moran.ruby@gmail.com\> wrote:
> 
> > I am behind a proxy server at work, yet got this to work. I commented  
> > out your Net::HTTP.new line and added what I believe to be the Proxy  
> > equivalent. Then I set Mozilla Firefox's proxy to be localhost:8080.  
> > NOTE: I do not have the ability to use another machine to test as I  
> > am very restricted with net access here. I launched slashdot.org and  
> > monitored the proxy.rb window while all of the HTML flew by. It came  
> > up and rendered properly in the browser.
> > 
> > I am on Windows 2000  
> > C:\\>ruby -v  
> > ruby 1.8.2 (2004-12-25) [i386-mswin32]
> > 
> > Here's the code. Hope it helps:
> > 
> > require 'socket'  
> > require 'net/http'  
> > local\_host = 'localhost'  
> > local\_port = 8080
> > 
> > host = 'slashdot.org'  
> > server = TCPServer.new(local\_host, local\_port)  
> > while true  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Thread.start(server.accept) do |s|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data = ''  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while s.gets && !$\_.nil?  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data += $\_  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break if $\_ == "\r\n" || $\_ == "\n"  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p "====Received: #{data}"  
> > # net = Net::HTTP.new(host,80)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;net = Net::HTTP::Proxy("proxy", "80").start(host)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;response = net.get('/')  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s \<\<  
> > "HTTP/#{response.http\_version}#{response.code}/#{response.message}  
> > \r\n"  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p "====Got back"  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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nbsp;&nbsp;s.close # We're done with this request, bring on the next.  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > end
> > 
> > On 4/21/05, Tanner Burson \<tanner.burson@gmail.com\> wrote:  
> > \> I've been working on a proxy server implementation for a project idea  
> > \> I have. But I've found that it only seems to work on certain pages,  
> > \> and not on others. I've distilled the problem down to a simple  
> > \> implementation, but it still occurs. The code below will return a  
> > \> (mostly) correct website if pointed at 'www.msn.com' but if pointed at  
> > \> say 'slashdot.org' the browser refuses to display it, in both cases  
> > \> the page is loaded correctly. If anyone has any ideas I'd really  
> > \> appreciate it.  
> > \>  
> > \> ===proxy.rb  
> > \>  
> > \> require 'socket'  
> > \> require 'net/http'  
> > \> local\_host = '192.168.0.6'  
> > \> local\_port = 8080  
> > \>  
> > \> host = 'slashdot.org'  
> > \> server = TCPServer.new(local\_host,local\_port)  
> > \> while true  
> > \> Thread.start(server.accept) do |s|  
> > \> data = ''  
> > \> while s.gets && !$\_.nil?  
> > \> data += $\_  
> > \> break if $\_ == "\r\n" || $\_ == "\n"  
> > \> end  
> > \> p "====Received: #{data}"  
> > \> net = Net::HTTP.new(host,80)  
> > \> response = net.get('/')  
> > \> s \<\< "HTTP/#{response.http\_version}  
> > \> #{response.code}/#{response.message} \r\n"  
> > \> p "====Got back"  
> > \> response.each\_header do |key,value|  
> > \> tmp = key + ": " + value + "\r\n"  
> > \> p tmp  
> > \> s \<\< tmp  
> > \> end  
> > \> p response.body  
> > \> s \<\< "\r\n" \<\< response.body  
> > \> s.close # We're done with this request, bring on the next.  
> > \> end  
> > \> end  
> > \>  
> > \> ====end  
> > \> --  
> > \> ===Tanner Burson===  
> > \> tanner.burson@gmail.com  
> > \> [http://tannerburson.com](http://tannerburson.com) \<---Might even work one day...  
> > \>  
> > \>

---

<div class="post-metadata">

**Author:** ![Tanner\_Burson](https://avatars.discourse-cdn.com/v4/letter/t/4bbf92/32.png) [@Tanner\_Burson](https://rubytalk.org/u/Tanner_Burson)\
**Post date:** [22 April 2005 01:51 UTC](https://rubytalk.org/t/proxy-server-troubles/17811/8 "2005-04-22T01:51:27Z")

</div>

> jm wrote:  
> \> I gave it a quick run. I had to change  
> \> local\_host = nil  
> \> so that it would bind and fixed the line wrap in the posted code.  
> \> Telnetted to port 8080 on localhost and hit enter twice and it came back  
> \> with a lot of html from slashdot.
> 
> I managed the same, on win32, but could not get the results to render in  
> Firefox or IE. It fetches the page OK (judging by the debugging p  
> calls), but whatever is sent back to the browser is off in some way.

This so far has been exactly the problem. Some sites (ie: msn.com )  
work just fine, but others won't work at all. I've compared the  
output of both requests but haven't been able to determine any  
difference other than possibly the amount of content being sent back.  
I'm about at my wit's end over this one, any advice would be great.

> **···**
>
> On 4/21/05, James Britt \<james\_b@neurogami.com\> wrote:
> 
> > James
> 
> --  
> ===Tanner Burson===  
> tanner.burson@gmail.com  
> [http://tannerburson.com](http://tannerburson.com) \<---Might even work one day...

---

<div class="post-metadata">

**Author:** ![Jim\_Freeze2](https://avatars.discourse-cdn.com/v4/letter/j/e480ec/32.png) [@Jim\_Freeze2](https://rubytalk.org/u/Jim_Freeze2)\
**Post date:** [22 April 2005 02:26 UTC](https://rubytalk.org/t/proxy-server-troubles/17811/9 "2005-04-22T02:26:48Z")

</div>

\* Tanner Burson \<[tanner.burson@gmail.com](mailto:tanner.burson@gmail.com)\> [2005-04-22 10:49:35 +0900]:

Have you tried setting your HTTP\_USER\_AGENT to something like:

&nbsp;&nbsp;Mozilla/5.0

> **···**
>
> --  
> Jim Freeze  
> Code Red. Code Ruby

---

<div class="post-metadata">

**Author:** ![jm11](https://avatars.discourse-cdn.com/v4/letter/j/b19c9b/32.png) [@jm11](https://rubytalk.org/u/jm11)\
**Post date:** [22 April 2005 03:50 UTC](https://rubytalk.org/t/proxy-server-troubles/17811/10 "2005-04-22T03:50:56Z")

</div>

Alright. this is wierd I just tried it with firefox on macosx and it rendered find, except for all the broken images, etc due to the reference to '/', ie

net = Net::HTTP.new(host,80)  
response = net.get('/')

Made a few modification. Really needs a clean up and there's probably libraries out there which will do this cleaner. I've been staring at perl code most of the day wishing it wasn't . Anyway, I've included the full script below.

require 'socket'  
require 'net/http'  
local\_host = nil  
local\_port = 8080

> **···**
>
> On 22/04/2005, at 11:48 AM, James Britt wrote:
> 
> > jm wrote:
> > 
> > > I gave it a quick run. I had to change  
> > > local\_host = nil  
> > > so that it would bind and fixed the line wrap in the posted code. Telnetted to port 8080 on localhost and hit enter twice and it came back with a lot of html from slashdot.
> > 
> > I managed the same, on win32, but could not get the results to render in Firefox or IE. It fetches the page OK (judging by the debugging p calls), but whatever is sent back to the browser is off in some way.
> 
> #  
> # returns proto,host,path or nil  
> #  
> def url\_split(h)  
> &nbsp;&nbsp;&nbsp;reg = Regexp.new('^(\w\*)\:\/\/([\w\.]+)(\/.\*)')  
> &nbsp;&nbsp;&nbsp;md = reg.match(h)  
> &nbsp;&nbsp;&nbsp;return nil unless md  
> &nbsp;&nbsp;&nbsp;return md[1],md[2],md[3]  
> end
> 
> host = 'slashdot.org'  
> server = TCPServer.new(local\_host,local\_port)  
> while true  
> &nbsp;&nbsp;&nbsp;Thread.start(server.accept) do |s|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;header = ''  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data = ''  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# this is a little crude  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;header = s.gets # added  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while s.gets && !$\_.nil?  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data += $\_  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break if $\_ == "\r\n" || $\_ == "\n"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;header = header.split(' ') # added  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proto,host,path = url\_split(header[1]) # added  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p "==== header[1]: #{header[1]}" # added  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p "==== #{proto} #{host} #{path}" # added  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p "====Received: #{data}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;net = Net::HTTP.new(host,80)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;response = net.get(path) # changed  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s \<\< "HTTP/#{response.http\_version} #{response.code}/#{response.message} \r\n"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p "====Got back"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;response.each\_header do |key,value|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tmp = key + ": " + value + "\r\n"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tmp = key + ": " + value + "\r\n"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p tmp  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s \<\< tmp  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p response.body  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s \<\< "\r\n" \<\< response.body  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s.close # We're done with this request, bring on the next.  
> &nbsp;&nbsp;&nbsp;end  
> end

---

<div class="post-metadata">

**Author:** ![Tanner\_Burson](https://avatars.discourse-cdn.com/v4/letter/t/4bbf92/32.png) [@Tanner\_Burson](https://rubytalk.org/u/Tanner_Burson)\
**Post date:** [25 April 2005 14:28 UTC](https://rubytalk.org/t/proxy-server-troubles/17811/11 "2005-04-25T14:28:39Z")

</div>

> I neglected to mention that if you use the proxy version of Net::HTTP,  
> you can get it to work without a proxy by putting in "" for the proxy  
> and port. Here's the proxy version:
> 
> net = Net::HTTP::Proxy("proxy", "80").start(host)
> 
> and the non-proxy version:
> 
> net = Net::HTTP::Proxy("", "").start(host)

This hangs until the timeout is reached. But if I use nil instead of  
empty string it continues, but still fails loading correctly in the  
browser. But I think it may have to do with the headers sent with a  
proxied request, can you send me the sent headers portion of the  
output when used with a working proxy server?

> **···**
>
> On 4/25/05, Craig Moran \<craig.m.moran.ruby@gmail.com\> wrote:
> 
> > HTH,  
> > Craig

---

<div class="post-metadata">

**Author:** ![Tanner\_Burson](https://avatars.discourse-cdn.com/v4/letter/t/4bbf92/32.png) [@Tanner\_Burson](https://rubytalk.org/u/Tanner_Burson)\
**Post date:** [22 April 2005 02:32 UTC](https://rubytalk.org/t/proxy-server-troubles/17811/12 "2005-04-22T02:32:45Z")

</div>

> \* Tanner Burson \<tanner.burson@gmail.com\> [2005-04-22 10:49:35 +0900]:
> 
> Have you tried setting your HTTP\_USER\_AGENT to something like:
> 
> &nbsp;&nbsp;Mozilla/5.0

When I'm using the full script (not included for brevity), it runs as  
a true proxy and relays all the request headers straight on through to  
the server. The code I provided is just a simple sample that still  
portrays the same problems. It doesn't appear to be anything related  
to the request itself, as the response comes back successfully. There  
is just something about it the browser doesn't like.

My first thought was that it didn't like receiving the whole page at  
once, so I tried reading the body in chunks and sending it on, but it  
didn't seem to solve it either.

> **···**
>
> On 4/21/05, Jim Freeze \<jim@freeze.org\> wrote:
> 
> > --  
> > Jim Freeze  
> > Code Red. Code Ruby
> 
> --  
> ===Tanner Burson===  
> tanner.burson@gmail.com  
> [http://tannerburson.com](http://tannerburson.com) \<---Might even work one day...

---

<div class="post-metadata">

**Author:** ![Jim\_Freeze2](https://avatars.discourse-cdn.com/v4/letter/j/e480ec/32.png) [@Jim\_Freeze2](https://rubytalk.org/u/Jim_Freeze2)\
**Post date:** [22 April 2005 06:49 UTC](https://rubytalk.org/t/proxy-server-troubles/17811/13 "2005-04-22T06:49:23Z")

</div>

\* jm \<jeffm@ghostgun.com\> [2005-04-22 12:50:56 +0900]:

> Alright. this is wierd I just tried it with firefox on macosx and it  
> rendered find, except for all the broken images, etc due to the  
> reference to '/', ie

Uhh, this may be another dumb suggestion, but are you  
by chance sending inspect data with #p instead of #puts  
to the browser?

> **···**
>
> > &nbsp;&nbsp;&nbsp;&nbsp;p response.body  
> > &nbsp;&nbsp;&nbsp;&nbsp;s \<\< "\r\n" \<\< response.body  
> > &nbsp;&nbsp;&nbsp;&nbsp;s.close # We're done with this request, bring on the next.
> 
> --  
> Jim Freeze  
> Code Red. Code Ruby
