Http proxy, string chomp error is nil?

im building a proxy, im having a problem with processing the get request
i recive from the client.

the client will send the proxy the get request and then the proxy sends
out this same request to the servers...

so, when i recieve a request from the client, ill take this header and
strip out the Host: like this

header="GET / HTTP/1.1
Host: www.google.com
User-Agent: Mozilla/5.0 (Windows NT 5.1Firefox/8.0.1
Accept: text/html,application/xhtml+xml
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection: keep-alive"

host = header.to_a[1].chomp[6..-1] => "www.google.com"

this works every time in irb testing, but is inconsistent when my script
is running, the proxy works 90% of the time. when www.yahoo.com is
requested the script crashes with Host nil error but when yahoo.com is
request the script works fine...

GET / HTTP/1.1
Host: www.yahoo.com
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:8.0.1) Gecko/20100101
Firefox/8.0.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection: keep-alive
Cookie:
B=8tefbfp77l57r&b=4&d=YTsZzxppYELpjioeXJu1kVafe.sroIDDz5owJg--&s=db&i=AhdUsAlvafAjzjZtkcC1;
F=a=.fI9HRcMvTab.ukb24sGFXCNVEqUnV6kET4hAn3W1si7nfsvB8h_xSSa5wZoY8sju.Bj7s4-&b=e70h;
Y=v=1&n=35d31ktflt3pr&p=;
fpc=d=dZMgXDOItm2f.klxz2sfDYNxkFevm_0AxEXHNXANYwDgrN_qJwqGHO_HqDkoHNxq0pvLiqEAAyWQSqTRNpIu3OGRXqcZJHhFM1qhL2c.5g44dNEYfgRPoOqvbhzgmVRNu3ls8RgDrnajHWeNjMlMjgJL0hKAYP2NgN3WGc2sXKDveE3.iRyd3Rdj2.92W0Aa5JXw_Ak-&v=2;
CH=AgIG0TQcTwn4YAAbrGAAEuBgAA2HYAAwnWAAKZhgAD5KYAAo5DAIGeMwACbxYAAZpGAAIZ4=;
CRZY0=t=bn=0;
fpt=d=vfO8FVTXecx5JjCjNA4wAmkmvxmpDc6Yi4soM9nzzwrVETeqMnlSv6.mPz7ZxDZIIZMRRoNng458dpROFMiVMgMhe75Oa1KxF_mr6SIty4chG2kmCka7ic5aoYek6kpahiH8n1fEGX7b..nLsoVDXFsRRXInysWPDDdgKTz0rUtcHNme6jenB.PEFmRe1LG0FBW_azvS7kZlHCkaFY9KYAEQuMf9ghHzSMNKqR0x4jj3UYZUpo1_1Jl399Y3jdPaE1.3AOlOp_OLiEL4MGlFxJ4I6ngNa6W2rz90_4iYAcda3YxBKMTBIOA_F.XZfqF0HKnikNB3pS1ealSjTWduAxbpcTK93kZkCWFMjD.lqB4njH2rQrxGd._RE0BsVmArPDhYGaGrW4DRH4y94cyZT6zRQ.QONbxHkBT0llvWOWxh5HeOimkRCWk7d00vOGy6ZWaV4UW3ODHRcXJg8ZBqm9AOsCDTQdxtZtUqgR9IQ8mErUUVij1o11BejZKdyfikXQpPePvVdL0j&v=1;
fpc_s=d=QrlkhWXXLB2gA5NNdxa7E0M74Un3.BAO4CVRxPC0gLkU4meLdhn7rhveTTfVD21HxzGEU1hlZ2www.yahoo.com
wE5Lm4lUU96Ukeh_ROc96RcvX9nltXX0NKvDG659Jmpojt1Ndso04VmHdVXa82tVBNpkIBfzYouP5EvvIGbRw72cEYbLAy3WCpT_8qwOdM..ESYE_fPs8aXRvKvxL3rMrHc0XP8BGjOOmal0KCW9BglRpbBOX9MD5IGqIw4ZKFq5HSQssHooX30KaWjFmBd9ot.dYOaifjWL8R5i3q4aqQK4OLvIXKqPxrUujQmPCSrbbdQeU-&v=2;
HP=0

host = header.to_a[1].chomp[6..-1] => nil

···

--
Posted via http://www.ruby-forum.com/.

Do you realize that the headers in an HTTP request can come in any order?
There is absolutely no guarantee that Host: will be first. You're relying
on that assumption.

···

Bigmac Turdsplash <i8igmac@aim.com> wrote:

im building a proxy, im having a problem with processing the get request
i recive from the client.
...
this works every time in irb testing, but is inconsistent when my script
is running, the proxy works 90% of the time. when www.yahoo.com is
requested the script crashes with Host nil error but when yahoo.com is
request the script works fine...
...
host = header.to_a[1].chomp[6..-1] => nil

--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

Tim Roberts wrote in post #1040037:

im building a proxy, im having a problem with processing the get request
i recive from the client.
...
this works every time in irb testing, but is inconsistent when my script
is running, the proxy works 90% of the time. when www.yahoo.com is
requested the script crashes with Host nil error but when yahoo.com is
request the script works fine...
...
host = header.to_a[1].chomp[6..-1] => nil

Do you realize that the headers in an HTTP request can come in any
order?
There is absolutely no guarantee that Host: will be first. You're
relying
on that assumption.

I figured this out after testing a few browsers...
here is my little hack for scanning threw a header...

ping = finger.index("Host:")
pong = finger.index("\n",ping)
host = finger[ping..pong].gsub("Host:","")

So, i have a stable proxy running, My goal is to place at the top of
every page a banner or advertisement...

send_data_to_client response.sub("</title>","</titlE> Hellow world
advertisement")

this works, but the page loads improperly...

so i tried to change the content-length of the packet to zero... I also
tried to increase the size of content-length to make room for my
banner...

so far, the page fails to load properly...

I need more ideas!

···

Bigmac Turdsplash <i8igmac@aim.com> wrote:

--
Posted via http://www.ruby-forum.com/\.

I figured this out after testing a few browsers...
here is my little hack for scanning threw a header...

ping = finger.index("Host:")
pong = finger.index("\n",ping)
host = finger[ping..pong].gsub("Host:","")

So, i have a stable proxy running, My goal is to place at the top of
every page a banner or advertisement...

Your goal is slimy.

send_data_to_client response.sub("</title>","</titlE> Hellow world
advertisement")

this works, but the page loads improperly...

The <title> tag lives in the <head> part of the page. There isn't supposed
to be anything renderable in the <head> part. You should be looking for
the <body> tag, and insert your slime just after that. If there isn't one,
then just shove your slime as the very first thing.

so i tried to change the content-length of the packet to zero... I also
tried to increase the size of content-length to make room for my
banner...

You do need to update Content-Length.

···

Bigmac Turdsplash <i8igmac@aim.com> wrote:
--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

Tim Roberts wrote in post #1040307:

I figured this out after testing a few browsers...
here is my little hack for scanning threw a header...

ping = finger.index("Host:")
pong = finger.index("\n",ping)
host = finger[ping..pong].gsub("Host:","")

So, i have a stable proxy running, My goal is to place at the top of
every page a banner or advertisement...

Your goal is slimy.

send_data_to_client response.sub("</title>","</titlE> Hellow world
advertisement")

this works, but the page loads improperly...

The <title> tag lives in the <head> part of the page. There isn't
supposed
to be anything renderable in the <head> part. You should be looking for
the <body> tag, and insert your slime just after that. If there isn't
one,
then just shove your slime as the very first thing.

so i tried to change the content-length of the packet to zero... I also
tried to increase the size of content-length to make room for my
banner...

You do need to update Content-Length.

Ok, i see, with some testing i have been successful with modifying
content-length and adding data... i was doing this in the wrong order,
my algorithm will find the packet that includes content-length and also
includes <body>

i have been testing with alert("hello world")...

so, i have been replacing strings like <body> <head> <title> <script>
etc...

i would modify them like this
send response.sub("<body>", "<body>xxxxx).sub(cont_length,cont_length+5)

so, lets say on one page there are 5 packets that include c-length and
also include <body>... then there would be 5 alert messages displayed
when the page loads...

or, like above was said, what if <body> does not exist...

i would like to 'shove my slime as the first thing" but i dont see a
control point. how do i know when its the beginning and the end of a
page request... there might be 1 or 30 GET request for one page...

···

Bigmac Turdsplash <i8igmac@aim.com> wrote:

--
Posted via http://www.ruby-forum.com/\.

No, there's only one GET request for the HTML. All the rest of the
requests are for the images and Javascripts and stylesheets. You want to
modify the text/html request.

···

Bigmac Turdsplash <i8igmac@aim.com> wrote:

i would like to 'shove my slime as the first thing" but i dont see a
control point. how do i know when its the beginning and the end of a
page request... there might be 1 or 30 GET request for one page...

--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.