1

here is the request URL http://localhost:9009/?comd&user=kkc&mail=kkc@kkc.com

what are the modification need to do in the server perl script.

server-Perl-script

use IO::Socket;
use Net::hostent;       # for OO version of gethostbyaddr
$PORT = 9009;           # pick something not in use
$server = IO::Socket::INET->new( Proto     => 'tcp',
                                 LocalPort => $PORT,
                                 Listen    => SOMAXCONN,
                                 Reuse     => 1);

die "can't setup server" unless $server;
print "[Server $0 accepting clients]\n";
     while ($client = $server->accept()) 
     {
       $client->autoflush(1);
       print $client "Welcome to $0; type help for command list.\n";
       $hostinfo = gethostbyaddr($client->peeraddr);
       printf "[Connect from %s]\n", $hostinfo ? $hostinfo->name : $client->peerhost;
       print $client "Command? ";
       while ( <$client>) {
         next unless /\S/;       # blank line
            if (/comd/i )        { print  $client `dir`;      }
       } continue {
          print $client "Command? ";
       }
       close $client;
       print "client closed";
     }
ghostdog74
  • 327,991
  • 56
  • 259
  • 343
kkchaitu
  • 631
  • 2
  • 5
  • 24

1 Answers1

0

I assume that your script is not for production, but for homework or testing sometime. There are multiple very efficient web server solutions in/with Perl like Apache with CGIs or mod_perl, HTTP::Server::Simple and PSGI/Plack.

You'll also typically use a framework like Dancer, Mojo or Catalyst which does most of the boring standard stuff for you:

use Dancer;

get '/' => sub {
    return 'Hi there, you just visited host '.request->host.
        ' at port '.request->port.' asking for '.request->uri;
};

Back to your question: Your script is a interactive server while HTTP has a strict request and response structure:

  1. Client connects to server
  2. Client sends a request
  3. Server sends a response

You need to remove the interactive part and just wait for the client to start the conversation:

use IO::Socket;
use Net::hostent;       # for OO version of gethostbyaddr
$PORT = 9009;           # pick something not in use
$server = IO::Socket::INET->new( Proto     => 'tcp',
                                 LocalPort => $PORT,
                                 Listen    => SOMAXCONN,
                                 Reuse     => 1);

die "can't setup server" unless $server;
print "[Server $0 accepting clients]\n";
     while ($client = $server->accept()) 
     {
       $hostinfo = gethostbyaddr($client->peeraddr);

       # Read request up to a empty line
       my $request;
       while ( <$client>) {
         last unless /\S/;
         $request .= $_;
       }

       # Do something with the request

       # Send response
       print $client "Status: 200 OK\r\nContent-type: text/plain\r\n\r\n".$request;

       close $client;
       print "client closed";
     }

The server reads the full request from the client and returns a minimized HTTP header plus the original request.

Sebastian
  • 2,472
  • 1
  • 18
  • 31