| 1 | package CGI::Tiny::_Debug; | ||
| 2 | |||
| 3 | # This file is part of CGI::Tiny which is released under: | ||
| 4 | # The Artistic License 2.0 (GPL Compatible) | ||
| 5 | # See the documentation for CGI::Tiny for full license details. | ||
| 6 | |||
| 7 | use strict; | ||
| 8 | use warnings; | ||
| 9 | |||
| 10 | our $VERSION = '1.003'; | ||
| 11 | |||
| 12 | my %methods = (get => 1, head => 1, post => 1, put => 1, delete => 1); | ||
| 13 | sub debug_command { | ||
| 14 | my ($cgi, $argv) = @_; | ||
| 15 | return 1 unless @$argv; | ||
| 16 | my $command = shift @$argv; | ||
| 17 | if (exists $methods{$command}) { | ||
| 18 | $cgi->{debug_method} = uc $command; | ||
| 19 | $cgi->{debug_verbose} = 1 if uc($command) eq 'HEAD'; | ||
| 20 | |||
| 21 | require Getopt::Long; | ||
| 22 | Getopt::Long::Configure('default', 'gnu_getopt', 'no_ignore_case'); | ||
| 23 | Getopt::Long::GetOptionsFromArray($argv, | ||
| 24 | 'content|c=s' => \my $content, | ||
| 25 | 'cookie|C=s' => \my @cookies, | ||
| 26 | 'header|H=s' => \my @headers, | ||
| 27 | 'verbose|v' => \$cgi->{debug_verbose}, | ||
| 28 | ) or die "Failed to parse debug command options\n"; | ||
| 29 | |||
| 30 | my ($path) = @$argv; | ||
| 31 | |||
| 32 | if (defined $content) { | ||
| 33 | open my $in_fh, '<', \$content or die "Failed to open in-memory handle to request content: $!\n"; | ||
| 34 | $cgi->set_input_handle($in_fh); | ||
| 35 | } | ||
| 36 | |||
| 37 | foreach my $header (@headers) { | ||
| 38 | my ($name, $value) = split /\s*:\s*/, $header, 2; | ||
| 39 | next unless defined $value; | ||
| 40 | $name =~ tr/-/_/; | ||
| 41 | if (defined $ENV{"HTTP_\U$name"}) { | ||
| 42 | $ENV{"HTTP_\U$name"} .= ", $value"; | ||
| 43 | } else { | ||
| 44 | $ENV{"HTTP_\U$name"} = $value; | ||
| 45 | } | ||
| 46 | } | ||
| 47 | |||
| 48 | $ENV{HTTP_CONTENT_LENGTH} = length $content if defined $content; | ||
| 49 | $ENV{HTTP_CONTENT_LENGTH} = '' unless defined $ENV{HTTP_CONTENT_LENGTH}; | ||
| 50 | $ENV{HTTP_CONTENT_TYPE} = 'application/octet-stream' if $ENV{HTTP_CONTENT_LENGTH} and !defined $ENV{HTTP_CONTENT_TYPE}; | ||
| 51 | $ENV{HTTP_COOKIE} = join '; ', @cookies if @cookies; | ||
| 52 | |||
| 53 | my $query; | ||
| 54 | ($path, $query) = split /\?/, $path, 2 if defined $path; | ||
| 55 | $ENV{AUTH_TYPE} = '' unless defined $ENV{AUTH_TYPE}; | ||
| 56 | $ENV{CONTENT_LENGTH} = $ENV{HTTP_CONTENT_LENGTH}; | ||
| 57 | $ENV{CONTENT_TYPE} = $ENV{HTTP_CONTENT_TYPE}; | ||
| 58 | $ENV{GATEWAY_INTERFACE} = 'CGI/1.1' unless defined $ENV{GATEWAY_INTERFACE}; | ||
| 59 | $ENV{PATH_INFO} = defined $path ? $path : ''; | ||
| 60 | $ENV{PATH_TRANSLATED} = '' unless defined $ENV{PATH_TRANSLATED}; | ||
| 61 | $ENV{QUERY_STRING} = defined $query ? $query : ''; | ||
| 62 | $ENV{REMOTE_ADDR} = '127.0.0.1' unless defined $ENV{REMOTE_ADDR}; | ||
| 63 | $ENV{REMOTE_HOST} = do { require Sys::Hostname; Sys::Hostname::hostname() } unless defined $ENV{REMOTE_HOST}; | ||
| 64 | $ENV{REMOTE_IDENT} = '' unless defined $ENV{REMOTE_IDENT}; | ||
| 65 | $ENV{REMOTE_USER} = '' unless defined $ENV{REMOTE_USER}; | ||
| 66 | $ENV{REQUEST_METHOD} = uc $command; | ||
| 67 | $ENV{SCRIPT_NAME} = do { require File::Basename; '/' . File::Basename::basename($0) } unless defined $ENV{SCRIPT_NAME}; | ||
| 68 | $ENV{SERVER_NAME} = do { require Sys::Hostname; Sys::Hostname::hostname() } unless defined $ENV{SERVER_NAME}; | ||
| 69 | $ENV{SERVER_PORT} = 80 unless defined $ENV{SERVER_PORT}; | ||
| 70 | $ENV{SERVER_PROTOCOL} = 'HTTP/1.0' unless defined $ENV{SERVER_PROTOCOL}; | ||
| 71 | $ENV{SERVER_SOFTWARE} = "CGI::Tiny/$VERSION" unless defined $ENV{SERVER_SOFTWARE}; | ||
| 72 | } else { | ||
| 73 | die "Unknown debug command $command\n"; | ||
| 74 | } | ||
| 75 | return 1; | ||
| 76 | } | ||
| 77 | |||
| 78 | 1; | ||
| 79 | |||
| 80 | =for Pod::Coverage *EVERYTHING* | ||
| 81 | |||
| 82 | =cut |