c43368101773f814910c91877a0b318e6aecf1cc
[catagits/fcgi2.git] / perl / eg / echo.pl
1 #!/usr/bin/perl
2
3 #  echo-perl --
4
5 #       Produce a page containing all FastCGI inputs
6
7 # Copyright (c) 1996 Open Market, Inc.
8 #
9 # See the file "LICENSE.TERMS" for information on usage and redistribution
10 # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
11
12 #  $Id: echo.PL,v 1.2 2000/12/14 13:46:23 skimo Exp $
13 #
14 # Changed by skimo to demostrate autoflushing 1997/02/19
15 #
16
17 use FCGI;
18 use strict;
19
20 sub print_env {
21     my($label, $envp) = @_;
22     print("$label:<br>\n<pre>\n");
23     my @keys = sort keys(%$envp);
24     foreach my $key (@keys) {
25         print("$key=$$envp{$key}\n");
26     }
27     print("</pre><p>\n");
28 }
29
30 my %env;
31 my $req = FCGI::Request(\*STDIN, \*STDOUT, \*STDERR, \%env);
32 my $count = 0;
33 while($req->Accept() >= 0) {
34     print("Content-type: text/html\r\n\r\n",
35           "<title>FastCGI echo (Perl)</title>\n",
36           "<h1>FastCGI echo (Perl)</h1>\n",
37           "Request number ", ++$count, "<p>\n");
38     my $len = 0 + $env{'CONTENT_LENGTH'};
39     if($len == 0) {
40         print("No data from standard input.<p>\n");
41     } else {
42         print("Standard input:<br>\n<pre>\n");
43         for(my $i = 0; $i < $len; $i++) {
44             my $ch = getc(STDIN);
45             if($ch eq "") {
46                 print("Error: Not enough bytes received ",
47                       "on standard input<p>\n");
48                 last;
49             }
50             print($ch);
51         }
52         print("\n</pre><p>\n");
53     }
54     print_env("Request environment", \%env);
55     print "More on its way ... wait a few seconds\n<BR>\n<BR>";
56     $req->Flush();
57     sleep(3);
58     print_env("Initial environment", \%ENV);
59     $req->Finish();
60 }