Delfer has asked for the wisdom of the Perl Monks concerning the following question:

I use SOCKET to listen to a port and I want to get the contents behind the URL. It looks like http://sth.domain.com:11111/?sth1&sth2

I want the "sth1&sth2".

MY SCRIPT:

#!/usr/local/bin/perl use strict; use Socket; use FileHandle; my $port = 11111; my $xxx=$ENV{"QUERY_STRING"}; 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)) { select(NEW_SOCKET); print "Port 11111 is available on my server"; print "Querystring is: <br>$xxx</br>"; close NEW_SOCKET; exit; } }
But result is "Querystring is:" only!
$xxx is NULL.
HOW CAN I GET THE "sth1&sth2"?I'm puzzled now.
Thanks

Replies are listed 'Best First'.
Easy!
by chromatic (Archbishop) on Feb 05, 2000 at 08:01 UTC
    First, you might want to try using HTTP::Daemon, as you can do something like this:
    $new_daemon = $daemon->accept(); my $r = $new_daemon->get_request; my $request = $r->url->epath;
    and $request will have what you want.

    You can also read from the NEW_SOCKET filehandle and parse the request yourself:

    my $line = (<NEW_SOCKET>); $line =~ m#GET http://(.*)/(.*)#;
    and you'll have good stuff in $1 and $2. Why do I know this? Been working on it lately.
Re: How do I get QUERY_STRING when I use sockets?
by stephen (Priest) on Feb 05, 2000 at 04:44 UTC
    Hmmm... So it looks to me like you've written your own web server. Thus, if you want CGI variables you'll need to get them yourself. That means you'll need to read the query sent by the browser and parse it. See the "Extracting a QUERY string" code in the snippets section; I've taken the liberty of patching in your code there. Also, you'll probably want to add the required HTTP headers to the output, so that an actual browser can look at it. (I'm assuming that browsers are looking at this, else why the query string? The query string is an artifact of the HTTP protocol.) I think also that there's a premade HTTP server module available on CPAN. You can probably adapt it for this use. Also, there's the URI module that comes with Perl; you can use that to parse the URL, and it will probably be more robust than my quick implementation. Good luck; we're all counting on you.
Re: How do I get QUERY_STRING when I use sockets?
by Anonymous Monk on Feb 05, 2000 at 04:30 UTC
    I'm not sure that I understand what you are asking, but unless I'm missing something special about 'Socket', I think you should just use one of the CGI modules... Have you tried: use CGI; my $cgi = new CGI; my $url = $cgi->param(shift($cgi->param()); or something else along those lines??? Good Luck 8-}