Perl

Moderators: Jonathan
Number of threads: 1236
Number of posts: 3605

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
HTTP response: printed as gibberish? Posted by gregster on 21 Nov 2003 at 10:49 AM
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)."\n";
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: \n";
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: \n";
print $modifiedfirstline;
$server_sock=server_connect($modifiedfirstline,$remote_host,$remote_port);
print $server_sock $modifiedfirstline;
while (defined($buf=<$client_sock>)) {
print "Received from Client: \n".$buf."\n";
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 =~ /^[\s\x00]*$/);
}
listen_for_response($server_sock, $client_sock);
}
}

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

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

# Remove remote hostname from URL
$request =~ s/http:\/\/[^\/]+//;
#$request .="\n\n";
#$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: \n$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 \n $buf \n";
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...\n";
# finished sending response to client, close sockets
close $server_sock;
close $client_sock;
}


Report
Re: HTTP response: printed as gibberish? Posted by Jonathan on 24 Nov 2003 at 1:56 PM
: 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.");




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.