call.pl 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #!/usr/bin/env perl
  2. # Use to call specific routes with the same configuration your website has
  3. use 5.040;
  4. use warnings FATAL => 'all';
  5. use re '/aa';
  6. use Data::Dumper;
  7. use FindBin::libs;
  8. use Carp::Always;
  9. #Grab our custom routes
  10. use TPSGI;
  11. =head1 USAGE
  12. call.pl METHOD ROUTE QUERY_STRING COOKIE REPEAT_X_TIMES MAX_REDIRECTS
  13. Max redirects default is 6.
  14. =cut
  15. my $host = $ENV{DOMAIN} // 'localhost';
  16. my %env = (
  17. REQUEST_METHOD => $ARGV[0],
  18. PATH_INFO => $ARGV[1],
  19. QUERY_STRING => $ARGV[2],
  20. REQUEST_URI => "http://$host/" . $ARGV[1],
  21. HTTP_HOST => $host,
  22. HTTP_COOKIE => $ARGV[3],
  23. 'psgi.errors' => *STDERR,
  24. 'psgi.input' => *STDIN,
  25. );
  26. sub emit_error {
  27. my ( $env, $err ) = @_;
  28. return $env->{'psgi.errors'}->print($err) if $env->{'psgi.errors'};
  29. print $err;
  30. return;
  31. }
  32. my %cfg;
  33. our $app = sub {
  34. # Allow re-run
  35. if (%cfg) {
  36. chdir( $cfg{tpsgi_dir} ) || warn "Can't chdir to $cfg{tpsgi_dir}: $_";
  37. }
  38. %cfg = TPSGI::get_config();
  39. # We are debugging here
  40. $cfg{verbose} = 1;
  41. # Change your dir into whatever basedir you need to be in
  42. chdir( $cfg{'basedir'} ) || warn "Can't chdir to $cfg{'basedir'}: $_";
  43. # If we have manually set the NYTPROF var, use it and don't try to control
  44. # When to stop or start it.
  45. $ENV{NYTPROF} ||= "sigexit=1:savesrc=0:start=no:file=$cfg{tpsgi_dir}/prof/nytprof.out";
  46. require Devel::NYTProf;
  47. mkdir "$cfg{tpsgi_dir}/prof";
  48. my $self = TPSGI->new(%cfg);
  49. local $@;
  50. return eval { $self->app(@_) } || do {
  51. my $env = shift;
  52. emit_error( $env, $@ );
  53. # Redact the stack trace past line 1, it usually has things which should not be shown
  54. $self->{cur_query}->{message} = $@;
  55. $self->{cur_query}->{message} =~ s/\n.*//gxs if $self->{cur_query}->{message};
  56. return $self->error( $self->{cur_query} );
  57. };
  58. };
  59. sub stream_to_stdout {
  60. my ($input) = @_;
  61. my ( $code, $headers ) = @$input;
  62. print "RC: $code\n", "Headers:\n", Dumper($headers);
  63. return \*STDOUT;
  64. }
  65. my $limit = $ARGV[4] || 1;
  66. my $max_redirects = $ARGV[5] || 6;
  67. my $redirects = 0;
  68. for ( 1 .. $limit ) {
  69. LOOP:
  70. my $out = $app->( \%env );
  71. if ( ref $out eq 'CODE' ) {
  72. $out->( \&stream_to_stdout );
  73. next;
  74. }
  75. if ( ref $out eq 'ARRAY' ) {
  76. # Might be a redirect, so check
  77. my %hdr = @{ $out->[1] };
  78. my $to = $hdr{Location};
  79. if ($to) {
  80. $env{REQUEST_URI} = "http://$host/$to";
  81. $env{PATH_INFO} = $to;
  82. $redirects++;
  83. die "Max redirects reached!" if $redirects >= $max_redirects;
  84. goto LOOP;
  85. }
  86. print $out->[2][0];
  87. next;
  88. }
  89. die "Weird return:\n" . Dumper($out);
  90. }