<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
  <channel>
    <title>'HTTP response: printed as gibberish?' Thread RSS Feed</title>
    <link>http://www.programmersheaven.com/</link>
    <description>Contains the latest posts from the thread 'HTTP response: printed as gibberish?' posted on the 'Perl' forum at Programmer's Heaven.</description>
    <language>en</language>
    <copyright>Copyright 2013 Programmers Heaven</copyright>
    <pubDate>Wed, 19 Jun 2013 07:20:42 -0700</pubDate>
    <lastBuildDate>Wed, 19 Jun 2013 07:20:42 -0700</lastBuildDate>
    <generator>Argotic Syndication Framework 2007.3.0.1, http://www.codeplex.com/Argotic</generator>
    <docs>http://www.rssboard.org/rss-specification</docs>
    <ttl>360</ttl>
    <image>
      <url>http://www.programmersheaven.com/images/ph.gif</url>
      <title>Programmers Heaven</title>
      <link>http://www.programmersheaven.com/</link>
      <width>88</width>
      <height>31</height>
    </image>
    <item>
      <title>HTTP response: printed as gibberish?</title>
      <link>http://www.programmersheaven.com/mb/perl/226319/226319/http-response-printed-as-gibberish/</link>
      <description>I've written a very short (and incomplete) HTTP proxy server. When I print out the HTTP response, it sometimes appears as gibberish, whereas sometimes they are printed out fine (is this an issue with big endian/little endian or something completely different?)&lt;br /&gt;
&lt;br /&gt;
#!/usr/bin/perl -w&lt;br /&gt;
&lt;br /&gt;
use IO::Socket;&lt;br /&gt;
&lt;br /&gt;
$SIG{PIPE} = "IGNORE";&lt;br /&gt;
&lt;br /&gt;
my $proxy_port=shift(@ARGV);&lt;br /&gt;
$proxy_port=8080 unless $proxy_port =~ /\d+/;&lt;br /&gt;
my $hostname=`hostname`;&lt;br /&gt;
chop $hostname;&lt;br /&gt;
&lt;br /&gt;
# Setup socket&lt;br /&gt;
$sock = new IO::Socket::INET (LocalHost =&amp;gt; $hostname,&lt;br /&gt;
                              LocalPort =&amp;gt; $proxy_port,&lt;br /&gt;
                              Proto     =&amp;gt; 'tcp',&lt;br /&gt;
                              Listen    =&amp;gt; 5,&lt;br /&gt;
                              Reuse     =&amp;gt; 1&lt;br /&gt;
                              );&lt;br /&gt;
die "Listening Socket could not be created: $!" unless $sock;&lt;br /&gt;
&lt;br /&gt;
# Accept connection request&lt;br /&gt;
while ($client_sock = $sock-&amp;gt;accept()) {&lt;br /&gt;
    $clientaddr = getpeername $client_sock ;&lt;br /&gt;
    ($port,$iaddr) = sockaddr_in($clientaddr);&lt;br /&gt;
    $clientname = gethostbyaddr ($iaddr, AF_INET);&lt;br /&gt;
    print "Received Connection Request from: ".inet_ntoa($iaddr)."\n";&lt;br /&gt;
    open CLOG, "&amp;gt;/tmp/clientlog".inet_ntoa($iaddr).".txt" || die "couldn't create client log txt";&lt;br /&gt;
    open SLOG, "&amp;gt;/tmp/serverlog".inet_ntoa($iaddr).".txt" || die "couldn't create server log txt";&lt;br /&gt;
    close CLOG; close SLOG;&lt;br /&gt;
&lt;br /&gt;
    # Create child process&lt;br /&gt;
    if (fork) {&lt;br /&gt;
        wait;&lt;br /&gt;
        next;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    # Receive first line of request&lt;br /&gt;
    if (defined($firstline = &amp;lt;$client_sock&amp;gt;)) {&lt;br /&gt;
        print "Received an HTTP request with first line: \n";&lt;br /&gt;
        print $firstline;&lt;br /&gt;
        open CLOG, "&amp;gt;/tmp/clientlog".inet_ntoa($iaddr).".txt" || die "couldn't create client log txt";&lt;br /&gt;
        print CLOG $firstline;&lt;br /&gt;
        close CLOG;&lt;br /&gt;
        ($modifiedfirstline,$remote_host,$remote_port)=ana
lyze_request($firstline);&lt;br /&gt;
 print "Modified firstline: \n";&lt;br /&gt;
        print $modifiedfirstline;&lt;br /&gt;
        $server_sock=server_connect($modifiedfirstline,$re
mote_host,$remote_port);&lt;br /&gt;
        print $server_sock $modifiedfirstline;&lt;br /&gt;
        while (defined($buf=&amp;lt;$client_sock&amp;gt;)) {&lt;br /&gt;
            print "Received from Client: \n".$buf."\n";&lt;br /&gt;
            open CLOG, "&amp;gt;&amp;gt;/tmp/clientlog".inet_ntoa($iaddr).".txt" || die "couldn't create client log txt";&lt;br /&gt;
            print CLOG $buf;&lt;br /&gt;
            close CLOG;&lt;br /&gt;
            next if ($buf =~ /Proxy-Connection:/);&lt;br /&gt;
            print $server_sock $buf;&lt;br /&gt;
            last if ($buf =~ /^[\s\x00]*$/);&lt;br /&gt;
        }&lt;br /&gt;
        listen_for_response($server_sock, $client_sock);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
sub analyze_request {&lt;br /&gt;
    ($request)=@_;&lt;br /&gt;
    print "Received this request:\n $request\n";&lt;br /&gt;
    if ($request =~ m#(GET|POST|HEAD) http://([^/:]+):?(\d*)#) {&lt;br /&gt;
        $method=$1;&lt;br /&gt;
        $remote_host=$2;&lt;br /&gt;
        $remote_port=$3;&lt;br /&gt;
        print "Recognized HTTP request \n";&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    print "Received request for remote_host $remote_host at port $remote_port...\n";&lt;br /&gt;
&lt;br /&gt;
    # Remove remote hostname from URL&lt;br /&gt;
    $request =~ s/http:\/\/[^\/]+//;&lt;br /&gt;
    #$request .="\n\n";&lt;br /&gt;
    #$request =~ s/(GET|POST|HEAD)(\s*)(HTTP(.*))/$1 index.html $3/ ;&lt;br /&gt;
&lt;br /&gt;
    return $request,$remote_host,$remote_port;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
sub server_connect {&lt;br /&gt;
    ($request,$remote_host,$remote_port) = @_;&lt;br /&gt;
    if ($remote_port !~ /^\d+$/) {&lt;br /&gt;
        $remote_port = 80;&lt;br /&gt;
    }&lt;br /&gt;
    $server_sock = new IO::Socket::INET (PeerAddr =&amp;gt; $remote_host,&lt;br /&gt;
                                         PeerPort =&amp;gt; $remote_port,&lt;br /&gt;
                                         Proto    =&amp;gt; 'tcp',&lt;br /&gt;
                                         );&lt;br /&gt;
    die "Server Socket could not be created: $!" unless $server_sock;&lt;br /&gt;
    print "Connected to server $remote_host at port $remote_port";&lt;br /&gt;
    print "Request sent to server: \n$request";&lt;br /&gt;
    #print $server_sock $request;&lt;br /&gt;
    return $server_sock;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
sub listen_for_response {&lt;br /&gt;
    ($server_sock, $client_sock) = @_;&lt;br /&gt;
    while (defined($buf = &amp;lt;$server_sock&amp;gt;)) {&lt;br /&gt;
        print "Received response from server \n $buf \n";&lt;br /&gt;
        open SLOG, "&amp;gt;&amp;gt;/tmp/serverlog".inet_ntoa($iaddr).".txt" || die "couldn't create server log txt";&lt;br /&gt;
        print SLOG $buf;&lt;br /&gt;
        close SLOG;&lt;br /&gt;
        print $client_sock $buf;&lt;br /&gt;
    }&lt;br /&gt;
    print "Finished receiving...\n";&lt;br /&gt;
    # finished sending response to client, close sockets&lt;br /&gt;
    close $server_sock;&lt;br /&gt;
    close $client_sock;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/perl/226319/226319/http-response-printed-as-gibberish/</guid>
      <pubDate>Fri, 21 Nov 2003 10:49:10 -0700</pubDate>
      <category>Perl</category>
    </item>
    <item>
      <title>Re: HTTP response: printed as gibberish?</title>
      <link>http://www.programmersheaven.com/mb/perl/226319/227079/re-http-response-printed-as-gibberish/#227079</link>
      <description>: I've written a very short (and incomplete) HTTP proxy server. When I &lt;br /&gt;
: print out the HTTP response, it sometimes appears as gibberish, &lt;br /&gt;
: whereas sometimes they are printed out fine (is this an issue with &lt;br /&gt;
: big endian/little endian or something completely different?)&lt;br /&gt;
I looked over your code and nothing stands out as being obviously wrong.  I'm not too convinced over the endian thing, though it could be that. I also wonder if HTTP compression could come into it.  What is gibberish, out of interest?  Is it:-&lt;br /&gt;
&lt;br /&gt;
The HTTP Response Headers&lt;br /&gt;
The Body&lt;br /&gt;
Both&lt;br /&gt;
&lt;br /&gt;
Go to netcraft and look at the info on the servers that you're having the fun with.  Is there any kind of OS/server software correlation?&lt;br /&gt;
&lt;br /&gt;
Just a few random thoughts that I can think off hand.  Could you maybe post some of the gibberish?  There's always an chance it might be recognisable gibberish. &lt;img src="http://www.programmersheaven.com/images/Community/twink.gif" width="15" height="15" alt="" /&gt;&lt;br /&gt;
&lt;br /&gt;
Jonathan&lt;br /&gt;
&lt;br /&gt;
###&lt;br /&gt;
for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&amp;amp;&amp;amp;&lt;br /&gt;
(tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;&lt;br /&gt;
/(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");&lt;br /&gt;
&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/perl/226319/227079/re-http-response-printed-as-gibberish/#227079</guid>
      <pubDate>Mon, 24 Nov 2003 13:56:37 -0700</pubDate>
      <category>Perl</category>
    </item>
  </channel>
</rss>