Here's a simple HTTP server that simply prints out the query string of the URL sent to it. Most of the code is by delfer; mine is just the query-string bit.
Consider this: <CODE> #!/usr/local/bin/perl use strict; use Socket; use FileHandle; my $port = 11111; socket(SOCKET, PF_INET, SOCK_STREAM, (getprotobyname('tcp'))[2]); bind (SOCKET, pack('Sna4x8', AF_INET, $port, "\0\0\0\0")); listen(SOCKET, 3); NEW_SOCKET->autoflush(); SOCKET->autoflush(); while (1) { accept(NEW_SOCKET, SOCKET); my $pid; if (! ($pid = fork)) { my $xxx = read_query(\*NEW_SOCKET); select(NEW_SOCKET); print "Port 11111 is available on my server\n"; print "Querystring is: <br>$xxx</br>\n"; close NEW_SOCKET; exit; } } sub read_query { my ($client_stream) = @_; my $whole_query = <$client_stream>; $whole_query =~ m{GET .*?\?(\S+)}; return $1; }
</CODE>

Replies are listed 'Best First'.
RE: Extracting a Query string
by Anonymous Monk on May 21, 2000 at 22:29 UTC
    There seems to be a much easier way using an ENV variable...
      The example is of using raw sockets and not an existing HTTP server. Since $ENV{QUERY_STRING} is set by the server *from* data read from the raw socket, it won't be available in this example until it's made available.

      I took the code as more of an example on how one could use sockets in Perl than how one ought to do web programming.