github.com/yanyiwu/go@v0.0.0-20150106053140-03d6637dbb7f/src/net/http/cgi/testdata/test.cgi (about)

     1  #!/usr/bin/perl
     2  # Copyright 2011 The Go Authors. All rights reserved.
     3  # Use of this source code is governed by a BSD-style
     4  # license that can be found in the LICENSE file.
     5  #
     6  # Test script run as a child process under cgi_test.go
     7  
     8  use strict;
     9  use Cwd;
    10  
    11  binmode STDOUT;
    12  
    13  my $q = MiniCGI->new;
    14  my $params = $q->Vars;
    15  
    16  if ($params->{"loc"}) {
    17      print "Location: $params->{loc}\r\n\r\n";
    18      exit(0);
    19  }
    20  
    21  print "Content-Type: text/html\r\n";
    22  print "X-CGI-Pid: $$\r\n";
    23  print "X-Test-Header: X-Test-Value\r\n";
    24  print "\r\n";
    25  
    26  if ($params->{"bigresponse"}) {
    27      # 17 MB, for OS X: golang.org/issue/4958
    28      for (1..(17 * 1024)) {
    29          print "A" x 1024, "\r\n";
    30      }
    31      exit 0;
    32  }
    33  
    34  print "test=Hello CGI\r\n";
    35  
    36  foreach my $k (sort keys %$params) {
    37      print "param-$k=$params->{$k}\r\n";
    38  }
    39  
    40  foreach my $k (sort keys %ENV) {
    41      my $clean_env = $ENV{$k};
    42      $clean_env =~ s/[\n\r]//g;
    43      print "env-$k=$clean_env\r\n";
    44  }
    45  
    46  # NOTE: msys perl returns /c/go/src/... not C:\go\....
    47  my $dir = getcwd();
    48  if ($^O eq 'MSWin32' || $^O eq 'msys') {
    49      if ($dir =~ /^.:/) {
    50          $dir =~ s!/!\\!g;
    51      } else {
    52          my $cmd = $ENV{'COMSPEC'} || 'c:\\windows\\system32\\cmd.exe';
    53          $cmd =~ s!\\!/!g;
    54          $dir = `$cmd /c cd`;
    55          chomp $dir;
    56      }
    57  }
    58  print "cwd=$dir\r\n";
    59  
    60  # A minimal version of CGI.pm, for people without the perl-modules
    61  # package installed.  (CGI.pm used to be part of the Perl core, but
    62  # some distros now bundle perl-base and perl-modules separately...)
    63  package MiniCGI;
    64  
    65  sub new {
    66      my $class = shift;
    67      return bless {}, $class;
    68  }
    69  
    70  sub Vars {
    71      my $self = shift;
    72      my $pairs;
    73      if ($ENV{CONTENT_LENGTH}) {
    74          $pairs = do { local $/; <STDIN> };
    75      } else {
    76          $pairs = $ENV{QUERY_STRING};
    77      }
    78      my $vars = {};
    79      foreach my $kv (split(/&/, $pairs)) {
    80          my ($k, $v) = split(/=/, $kv, 2);
    81          $vars->{_urldecode($k)} = _urldecode($v);
    82      }
    83      return $vars;
    84  }
    85  
    86  sub _urldecode {
    87      my $v = shift;
    88      $v =~ tr/+/ /;
    89      $v =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
    90      return $v;
    91  }