Plack-App-CLI

 view release on metacpan or  search on metacpan

lib/Plack/App/CLI.pm  view on Meta::CPAN

package Plack::App::CLI;

use strict; 
use warnings; 

our $VERSION = '0.1';

use Getopt::Std;
use Term::ANSIColor;
use IO::Handle;
use URI;

use Plack;
use Plack::TempBuffer; 
use Plack::Util;

my $colors = 1; 
my $output = IO::Handle->new_from_fd(fileno(STDOUT), "w");
my $error = IO::Handle->new_from_fd(fileno(STDERR), "w");
my $headers = 0; 

use constant {
    STATUS  => 0,
    HEADERS => 1,
    BODY    => 2
};

my $env = {
	HTTP_ACCEPT => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
	HTTP_USER_AGENT => "Plack $Plack::VERSION, CLI $VERSION",
	HTTP_CACHE_CONTROL => 'max-age=0',
	HTTP_ACCEPT_LANGUAGE => 'en-US,en;q=0.8',
	HTTP_ACCEPT_ENCODING => 'gzip,deflate,sdch',
	HTTP_ACCEPT_CHARSET => 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',

        # Client
        REQUEST_METHOD => 'GET',
        SCRIPT_NAME    => '',
        REMOTE_ADDR    => '0.0.0.0',
        REMOTE_USER    => $ENV{USER},

        # Server
        SERVER_PROTOCOL => 'HTTP/1.0',
        SERVER_PORT     => 0,
        SERVER_NAME     => 'localhost',

        # PSGI
        'psgi.version'      => [1,1],
        'psgi.errors'       => $error,
        'psgi.multithread'  => Plack::Util::FALSE,
        'psgi.multiprocess' => Plack::Util::TRUE,
        'psgi.run_once'     => Plack::Util::TRUE,
        'psgi.streaming'    => Plack::Util::FALSE,
        'psgi.nonblocking'  => Plack::Util::FALSE,

        %ENV
};

sub run {
 	my($self, $app) = @_;

	if ( ref $app ne "CODE" ) {
		$app = Plack::Util::load_psgi($app);
	} 
	
	my $options = {};
	my $environment = "deployment";

	getopts("E:X:o:H:vnh", $options);

	if ( exists $options->{E} ) {
		$environment = $options->{E};
	}

	if ( $options->{v} ) {
		print $env->{HTTP_USER_AGENT}, "\n";
		exit(0);
	}

	if ( $options->{n} ) {
		$colors = 0; 
	}

	if ( $options->{h} ) {
		$headers = 1; 
	}

	my $method = "GET";

	my $request_uri = ( shift @ARGV || "/" );
	my $url = "http://localhost" . $request_uri;

	if ( exists $options->{X} ) {
		$method = $options->{X};
	}

	if ( exists $options->{o} ) {
		$output = new IO::File "> $options->{o}";
		$colors = 0; 
	}

	if ( $method eq "POST" ) {
		my $data = "";
		while ( <STDIN> ) { chomp; $data .= $_; }

		my $len = length $data; 
		my $buf = new Plack::TempBuffer($len);
		$buf->print($data);

		$env->{CONTENT_LENGTH} = $len;
		$env->{CONTENT_TYPE} = "application/x-www-form-urlencoded"; 
		$env->{'psgi.input'} = $buf->rewind;
	} elsif ( $method eq "GET"  ) {



( run in 0.640 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )