Find the answer to your Linux question:
Results 1 to 7 of 7
was learning perl cgi programing & without web server installed looking for simple way to render cgi to browser.... Code: #!/bin/bash PAGE=`perl -T $at` echo $PAGE echo $PAGE | perl ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jul 2012
    Posts
    10

    Problems with Perl CGI losing format when backtrick


    was learning perl cgi programing & without web server installed looking for simple way to render cgi to browser....

    Code:
    #!/bin/bash
    PAGE=`perl -T $at`
    echo $PAGE
    echo $PAGE | perl -e 'my $h=0; while(<>){ if(/\</){ $h=1; } if($h){ print; }}' > tmppage.htm
    firefox tmppage.htm
    sample cgi like this
    Code:
    #!/usr/bin/perl -wT
    use strict;
    use CGI::Pretty;
    
    my $query = CGI->new();
    
    print $query->header( "text/html" ),
      $query->start_html(-title   => "Title"),
      $query->p( "blah..blah" ),
      $query->end_html;
    prints out unformated & is missing "Content-Type: text/html" line
    Code:
     <!DOCTYPE html ...........ional.dtd"> <html xmlns="http...............xhtml" lang="en-US" xml:lang="en-US"> <head> <title>Title</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> </head> <body> <p> blah..blah </p> </body> </html>

  2. #2
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,307
    to get around that, you could try printf instead of echo, e.g.:

    Code:
    #!/bin/bash
    PAGE=`perl -T $at`
    #echo $PAGE
    #echo $PAGE | perl -e 'my $h=0; while(<>){ if(/\</){ $h=1; } if($h){ print; }}' > tmppage.htm
    printf "$PAGE\n" | perl -e 'my $h=0; while(<>){ if(/\</){ $h=1; } if($h){ print; }}' > tmppage.htm
    however, if $at is a script containing the perl code you included, why not just do this, from the terminal?

    Code:
    perl script.pl > tmppage.htm

  3. #3
    Just Joined!
    Join Date
    Jul 2012
    Posts
    10
    work perfect..completely forgot about printf

    as to why I did it this way it was getting messy passing params, also wanted to print exactly what was being outputted & see on the browser also...
    & the extra content line on the browser bothers me.

  4. #4
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,307
    also, don't know how you are going to pass CGI parameters to a CGI script without a server installed...but if you do have one installed, and your CGI script is in the proper place (e.g., /var/www/cgi-bin), you can use the terminal based browser lynx to test your CGI script.

    for example, here's a simple CGI script that takes parameters:

    Code:
    #!/usr/bin/perl
    use strict;
    use warnings;
    use CGI;
    print "Content-type: text/html\n\n";
    my $cgi = new CGI;
    for($cgi->param()){
      print $_," => ",$cgi->param($_),"<BR>\n";
    }
    you could then test it like this, in a terminal (assuming your web server is running):
    Code:
    lynx -dump 'http://localhost/cgi-bin/cgi.pl?foo=bar&blah=meh'
    you should get output that looks like this:
    Code:
       foo => bar
       blah => meh

  5. #5
    Just Joined!
    Join Date
    Jul 2012
    Posts
    10
    I was passing it on the command line like:
    Code:
    perl -T cgi.pl foo=bar blah=meh
    But then seeing the socket mod came up with...its rough & hangs some times....
    Code:
    #!/usr/bin/perl
    use Socket;
    
    $port = 8080;
    $ip = inet_aton('localhost');
    ($name, $aliases, $protocol) = getprotobyname('tcp');
    
    socket(S,AF_INET,SOCK_STREAM,$protocol) || die "socket : $!";
    $s_addrin = sockaddr_in($port, $ip);
    bind(S, $s_addrin) || die "bind : $!";
    listen(S,2) || die "listen: $!";
    
    $SIG{INT} = sub { shutdown S, 2;  die "SERVER STOPPED"; };
    
    for ($con = 1; ; $con++) {
    	printf("Waiting for connection %d....\n", $con);
    	($addr = accept(NS,S)) || die $!;
    
    	if (($child = fork()) == 0) { # We're the child...
    		childproc(NS, $con, $addr);
    		exit;
    	}
    	# this is where the parent returns; 
    	close(NS);
    }
    
    sub childproc
    {
    	my ($NS, $con, $addr) = @_;
    
    	print "Serving connection $con @ Internet address ....\n";
    	my $request = "";
    
    	while (<$NS>) {
    		if(/^\s*$/){last;}
    		if(/^[A-Z]+\s+(.*)\s+HTTP/){ $request = $1; }
    		print "  Received from client $con: $_";
      }
    	print "REQUESTED = $request --- ";	
    	$request =~ /([^\/]+$)/; 
    	$request = $1 || 'index.htm';
    	print "SERVING = $request\n";
    
    	if($request =~ /^(\w+\.htm)/ && -e $1){
    		local( $/, *FH);
    		open(FH, $1) ||  die "SERVER ERROR\n";  
    		my $page = <FH>;
    		close(FH);
    
    		print $NS "HTTP/1.1 200 OK\nServer: PSS 0.0\nContent-Type: text/html\n\n";
    		print $NS "$page\n\n";
    	}
    	elsif($request =~ /^(\w+\.cgi)(\?(.*))?/ && -e $1){
    		$ENV{REQUEST_METHOD} = 'GET';
    		$ENV{QUERY_STRING} = $3 || '';
    		my $page = `perl -T $1`;
    		print $NS "HTTP/1.1 200 OK\nServer: PSS 0.0\n";
    		print $NS "$page\n\n";
    	}
    	else{ 
    		print $NS "HTTP/1.1 404 NOT FOUND\nServer: PSS 0.0\nContent-Type: text/plain\n\n";
    		print $NS "UNABLE TO FULLFILL REQUEST\n\n";
    		print "client $con: 404\n";
     }
    
    	close($NS);
    }

  6. #6
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,307
    Quote Originally Posted by fzwd View Post
    I was passing it on the command line like:
    Code:
    perl -T cgi.pl foo=bar blah=meh
    i didn't even realize the perl CGI module would do it, but it parses the command line args as if they were sent via query string. so you could use the code I suggested that way too, e.g.:
    Code:
    [root@localhost]# cat cgi.pl
    #!/usr/bin/perl
    use strict;
    use warnings;
    use CGI;
    
    print "Content-type: text/html\n\n";
    
    my $cgi = new CGI;
    for($cgi->param()){
      print $_," => ",$cgi->param($_),"<BR>\n";
    }
    [root@localhost]#
    [root@localhost]# perl -T cgi.pl foo=bar meh=blah
    Content-type: text/html
    
    foo => bar<BR>
    meh => blah<BR>
    [root@localhost]#
    But then seeing the socket mod came up with...its rough & hangs some times....
    what is the point of that code?

  7. #7
    Just Joined!
    Join Date
    Jul 2012
    Posts
    10
    did it need commenting? the point? is its a local webserver ... maybe time should be spent on cgi programming...instead of debugging and hoping that the solutions I end up with are close enough how a real http server work ... So yes in the end testing should be done on something written on something thats been tested.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •