Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

HTTP response: printed as gibberish?

gregstergregster Posts: 7Member
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?)

#!/usr/bin/perl -w

use IO::Socket;

$SIG{PIPE} = "IGNORE";

my $proxy_port=shift(@ARGV);
$proxy_port=8080 unless $proxy_port =~ /d+/;
my $hostname=`hostname`;
chop $hostname;

# Setup socket
$sock = new IO::Socket::INET (LocalHost => $hostname,
LocalPort => $proxy_port,
Proto => 'tcp',
Listen => 5,
Reuse => 1
);
die "Listening Socket could not be created: $!" unless $sock;

# Accept connection request
while ($client_sock = $sock->accept()) {
$clientaddr = getpeername $client_sock ;
($port,$iaddr) = sockaddr_in($clientaddr);
$clientname = gethostbyaddr ($iaddr, AF_INET);
print "Received Connection Request from: ".inet_ntoa($iaddr)."
";
open CLOG, ">/tmp/clientlog".inet_ntoa($iaddr).".txt" || die "couldn't create client log txt";
open SLOG, ">/tmp/serverlog".inet_ntoa($iaddr).".txt" || die "couldn't create server log txt";
close CLOG; close SLOG;

# Create child process
if (fork) {
wait;
next;
}

# Receive first line of request
if (defined($firstline = <$client_sock>)) {
print "Received an HTTP request with first line:
";
print $firstline;
open CLOG, ">/tmp/clientlog".inet_ntoa($iaddr).".txt" || die "couldn't create client log txt";
print CLOG $firstline;
close CLOG;
($modifiedfirstline,$remote_host,$remote_port)=analyze_request($firstline);
print "Modified firstline:
";
print $modifiedfirstline;
$server_sock=server_connect($modifiedfirstline,$remote_host,$remote_port);
print $server_sock $modifiedfirstline;
while (defined($buf=<$client_sock>)) {
print "Received from Client:
".$buf."
";
open CLOG, ">>/tmp/clientlog".inet_ntoa($iaddr).".txt" || die "couldn't create client log txt";
print CLOG $buf;
close CLOG;
next if ($buf =~ /Proxy-Connection:/);
print $server_sock $buf;
last if ($buf =~ /^[sx00]*$/);
}
listen_for_response($server_sock, $client_sock);
}
}

sub analyze_request {
($request)=@_;
print "Received this request:
$request
";
if ($request =~ m#(GET|POST|HEAD) http://([^/:]+):?(d*)#) {
$method=$1;
$remote_host=$2;
$remote_port=$3;
print "Recognized HTTP request
";
}

print "Received request for remote_host $remote_host at port $remote_port...
";

# Remove remote hostname from URL
$request =~ s/http://[^/]+//;
#$request .="

";
#$request =~ s/(GET|POST|HEAD)(s*)(HTTP(.*))/$1 index.html $3/ ;

return $request,$remote_host,$remote_port;
}

sub server_connect {
($request,$remote_host,$remote_port) = @_;
if ($remote_port !~ /^d+$/) {
$remote_port = 80;
}
$server_sock = new IO::Socket::INET (PeerAddr => $remote_host,
PeerPort => $remote_port,
Proto => 'tcp',
);
die "Server Socket could not be created: $!" unless $server_sock;
print "Connected to server $remote_host at port $remote_port";
print "Request sent to server:
$request";
#print $server_sock $request;
return $server_sock;
}

sub listen_for_response {
($server_sock, $client_sock) = @_;
while (defined($buf = <$server_sock>)) {
print "Received response from server
$buf
";
open SLOG, ">>/tmp/serverlog".inet_ntoa($iaddr).".txt" || die "couldn't create server log txt";
print SLOG $buf;
close SLOG;
print $client_sock $buf;
}
print "Finished receiving...
";
# finished sending response to client, close sockets
close $server_sock;
close $client_sock;
}


Comments

  • JonathanJonathan Posts: 2,914Member
    : 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?)
    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:-

    The HTTP Response Headers
    The Body
    Both

    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?

    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. ;-)

    Jonathan

    ###
    for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");

Sign In or Register to comment.