File Coverage

blib/lib/Apache/Emulator/Apache.pm
Criterion Covered Total %
statement 5 104 4.8
branch 0 46 0.0
condition 0 23 0.0
subroutine 2 42 4.7
pod 0 40 0.0
total 7 255 2.7


line stmt bran cond sub pod time code
1             package Apache::Emulator::Apache;
2              
3             package Apache;
4 1     1   3504 use strict;
  1         2  
  1         1463  
5              
6             sub new {
7 0     0 0   my $class = shift;
8 0           my %p = @_;
9 0   0       return bless {
10             query => $p{cgi} || CGI->new,
11             headers_out => Apache::Table->new,
12             err_headers_out => Apache::Table->new,
13             pnotes => {},
14             }, $class;
15             }
16              
17             # CGI request are _always_ main, and there is never a previous or a next
18             # internal request.
19 0     0 0   sub main {}
20 0     0 0   sub prev {}
21 0     0 0   sub next {}
22 0     0 0   sub is_main {1}
23 0     0 0   sub is_initial_req {1}
24              
25             # What to do with this?
26             # sub allowed {}
27              
28             sub method {
29 0     0 0   $_[0]->query->request_method;
30             }
31              
32             # There mut be a mapping for this.
33             # sub method_number {}
34              
35             # Can CGI.pm tell us this?
36             # sub bytes_sent {0}
37              
38             # The request line sent by the client." Poached from Apache::Emulator.
39             sub the_request {
40 0     0 0   my $self = shift;
41 0 0 0       $self->{the_request} ||= join ' ', $self->method,
42             ( $self->{query}->query_string
43             ? $self->uri . '?' . $self->{query}->query_string
44             : $self->uri ),
45             $self->{query}->server_protocol;
46             }
47              
48             # Is CGI ever a proxy request?
49             # sub proxy_req {}
50              
51 0     0 0   sub header_only { $_[0]->method eq 'HEAD' }
52              
53 0 0   0 0   sub protocol { $ENV{SERVER_PROTOCOL} || 'HTTP/1.0' }
54              
55 0     0 0   sub hostname { $_[0]->{query}->server_name }
56              
57             # Fake it by just giving the current time.
58 0     0 0   sub request_time { time }
59              
60             sub uri {
61 0     0 0   my $self = shift;
62              
63 0   0       $self->{uri} ||= $self->{query}->script_name . $self->path_info || '';
      0        
64             }
65              
66             # Is this available in CGI?
67             # sub filename {}
68              
69             # "The $r->location method will return the path of the
70             # section from which the current "Perl*Handler"
71             # is being called." This is irrelevant, I think.
72             # sub location {}
73              
74 0     0 0   sub path_info { $_[0]->{query}->path_info }
75              
76             sub args {
77 0     0 0   my $self = shift;
78 0 0         if (@_) {
79             # Assign args here.
80             }
81 0 0         return $self->{query}->Vars unless wantarray;
82             # Do more here to return key => arg values.
83             }
84              
85             sub headers_in {
86 0     0 0   my $self = shift;
87              
88             # Create the headers table if necessary. Decided how to build it based on
89             # information here:
90             # http://cgi-spec.golux.com/draft-coar-cgi-v11-03-clean.html#6.1
91             #
92             # Try to get as much info as possible from CGI.pm, which has
93             # workarounds for things like the IIS PATH_INFO bug.
94             #
95 0           $self->{headers_in} ||= Apache::Table->new
96             ( 'Authorization' => $self->{query}->auth_type, # No credentials though.
97             'Content-Length' => $ENV{CONTENT_LENGTH},
98             'Content-Type' =>
99             ( $self->{query}->can('content_type') ?
100             $self->{query}->content_type :
101             $ENV{CONTENT_TYPE}
102             ),
103             # Convert HTTP environment variables back into their header names.
104             map {
105 0           my $k = ucfirst lc;
106 0           $k =~ s/_(.)/-\u$1/g;
107 0           ( $k => $self->{query}->http($_) )
108 0 0 0       } grep { s/^HTTP_// } keys %ENV
109             );
110              
111              
112             # Give 'em the hash list of the hash table.
113 0 0         return wantarray ? %{$self->{headers_in}} : $self->{headers_in};
  0            
114             }
115              
116             sub header_in {
117 0     0 0   my ($self, $header) = (shift, shift);
118 0           my $h = $self->headers_in;
119 0 0         return @_ ? $h->set($header, shift) : $h->get($header);
120             }
121              
122              
123             # The $r->content method will return the entity body
124             # read from the client, but only if the request content
125             # type is "application/x-www-form-urlencoded". When
126             # called in a scalar context, the entire string is
127             # returned. When called in a list context, a list of
128             # parsed key => value pairs are returned. *NOTE*: you
129             # can only ask for this once, as the entire body is read
130             # from the client.
131             # Not sure what to do with this one.
132             # sub content {}
133              
134             # I think this may be irrelevant under CGI.
135             # sub read {}
136              
137             # Use LWP?
138 0     0 0   sub get_remote_host {}
139 0     0 0   sub get_remote_logname {}
140              
141             sub http_header {
142 0     0 0   my $self = shift;
143 0           my $h = $self->headers_out;
144 0           my $e = $self->err_headers_out;
145 0 0 0       my $method = exists $h->{Location} || exists $e->{Location} ?
146             'redirect' : 'header';
147 0           return $self->query->$method(tied(%$h)->cgi_headers,
148             tied(%$e)->cgi_headers);
149             }
150              
151             sub send_http_header {
152 0     0 0   my $self = shift;
153              
154 0           print STDOUT $self->http_header;
155              
156 0           $self->{http_header_sent} = 1;
157             }
158              
159 0     0 0   sub http_header_sent { shift->{http_header_sent} }
160              
161             # How do we know this under CGI?
162             # sub get_basic_auth_pw {}
163             # sub note_basic_auth_failure {}
164              
165             # I think that this just has to be empty.
166 0     0 0   sub handler {}
167              
168             sub notes {
169 0     0 0   my ($self, $key) = (shift, shift);
170 0   0       $self->{notes} ||= Apache::Table->new;
171 0 0         return wantarray ? %{$self->{notes}} : $self->{notes}
  0 0          
172             unless defined $key;
173 0 0         return $self->{notes}{$key} = "$_[0]" if @_;
174 0           return $self->{notes}{$key};
175             }
176              
177             sub pnotes {
178 0     0 0   my ($self, $key) = (shift, shift);
179 0 0         return wantarray ? %{$self->{pnotes}} : $self->{pnotes}
  0 0          
180             unless defined $key;
181 0 0         return $self->{pnotes}{$key} = $_[0] if @_;
182 0           return $self->{pnotes}{$key};
183             }
184              
185             sub subprocess_env {
186 0     0 0   my ($self, $key) = (shift, shift);
187 0 0         unless (defined $key) {
188 0           $self->{subprocess_env} = Apache::Table->new(%ENV);
189 0 0         return wantarray ? %{$self->{subprocess_env}} :
  0            
190             $self->{subprocess_env};
191              
192             }
193 0   0       $self->{subprocess_env} ||= Apache::Table->new(%ENV);
194 0 0         return $self->{subprocess_env}{$key} = "$_[0]" if @_;
195 0           return $self->{subprocess_env}{$key};
196             }
197              
198             sub content_type {
199 0     0 0   shift->header_out('Content-Type', @_);
200             }
201              
202             sub content_encoding {
203 0     0 0   shift->header_out('Content-Encoding', @_);
204             }
205              
206             sub content_languages {
207 0     0 0   my ($self, $langs) = @_;
208 0 0         return unless $langs;
209 0           my $h = shift->headers_out;
210 0           for my $l (@$langs) {
211 0           $h->add('Content-Language', $l);
212             }
213             }
214              
215             sub status {
216 0     0 0   shift->header_out('Status', @_);
217             }
218              
219             sub status_line {
220             # What to do here? Should it be managed differently than status?
221 0     0 0   my $self = shift;
222 0 0         if (@_) {
223 0           my $status = shift =~ /^(\d+)/;
224 0           return $self->header_out('Status', $status);
225             }
226 0           return $self->header_out('Status');
227             }
228              
229             sub headers_out {
230 0     0 0   my $self = shift;
231 0 0         return wantarray ? %{$self->{headers_out}} : $self->{headers_out};
  0            
232             }
233              
234             sub header_out {
235 0     0 0   my ($self, $header) = (shift, shift);
236 0           my $h = $self->headers_out;
237 0 0         return @_ ? $h->set($header, shift) : $h->get($header);
238             }
239              
240             sub err_headers_out {
241 0     0 0   my $self = shift;
242 0 0         return wantarray ? %{$self->{err_headers_out}} : $self->{err_headers_out};
  0            
243             }
244              
245             sub err_header_out {
246 0     0 0   my ($self, $err_header) = (shift, shift);
247 0           my $h = $self->err_headers_out;
248 0 0         return @_ ? $h->set($err_header, shift) : $h->get($err_header);
249             }
250              
251             sub no_cache {
252 0     0 0   my $self = shift;
253 0           $self->header_out(Pragma => 'no-cache');
254 0           $self->header_out('Cache-Control' => 'no-cache');
255             }
256              
257             sub print {
258 0     0 0   print @_;
259             }
260              
261             sub send_fd {
262 0     0 0   my ($self, $fd) = @_;
263 0           local $_;
264              
265 0           print STDOUT while defined ($_ = <$fd>);
266             }
267              
268             # Should this perhaps throw an exception?
269             # sub internal_redirect {}
270             # sub internal_redirect_handler {}
271              
272             # Do something with ErrorDocument?
273             # sub custom_response {}
274              
275             # I think we'ev made this essentially the same thing.
276             BEGIN {
277 1     1   3 local $^W;
278 1         95 *send_cgi_header = \&send_http_header;
279             }
280              
281             # Does CGI support logging?
282             # sub log_reason {}
283             # sub log_error {}
284             sub warn {
285 0     0 0   shift;
286 0           print STDERR @_, "\n";
287             }
288              
289             sub params {
290 0     0 0   my $self = shift;
291 0           return HTML::Mason::Utils::cgi_request_args($self->query,
292             $self->query->request_method);
293             }
294              
295             1;
296              
297             __END__