/usr/share/perl5/Plack/Middleware/MethodOverride.pm is in libplack-middleware-methodoverride-perl 0.15-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | use 5.008001;
use strict;
use warnings;
use Plack::Request ();
package Plack::Middleware::MethodOverride;
$Plack::Middleware::MethodOverride::VERSION = '0.15';
# ABSTRACT: Override REST methods to Plack apps via POST
use parent 'Plack::Middleware';
use Plack::Util::Accessor 'param';
my %allowed_method = map { $_ => undef } qw(GET HEAD PUT DELETE OPTIONS TRACE CONNECT PATCH);
sub new {
my $class = shift;
my $self = $class->SUPER::new(@_);
$self->{param} = 'x-tunneled-method' unless exists $self->{param};
$self->{header} = 'X-HTTP-Method-Override' unless exists $self->{header};
$self->header($self->{header}); # munge it
return $self;
}
sub call {
my ($self, $env) = @_;
my $meth = $env->{'plack.original_request_method'} = $env->{REQUEST_METHOD};
if ($meth and uc $meth eq 'POST') {
no warnings 'uninitialized';
my $override = uc (
$env->{$self->header}
or $env->{QUERY_STRING} && Plack::Request->new($env)->query_parameters->{$self->param}
);
$env->{REQUEST_METHOD} = $override if exists $allowed_method{$override};
}
$self->app->($env);
}
sub header {
my $self = shift;
return $self->{header} if not @_;
return $self->{header} = '' if not $_[0];
(my $key = 'HTTP_'.$_[0]) =~ tr/-a-z/_A-Z/;
return $self->{header} = $key;
}
1;
__END__
=pod
=encoding UTF-8
=head1 Name
Plack::Middleware::MethodOverride - Override REST methods to Plack apps via POST
=head1 Version
version 0.15
=head1 Synopsis
In your Plack app:
use Plack::Builder;
builder {
enable MethodOverride;
$app;
};
PUT via a query parameter in your POST forms:
<form method="POST" action="/foo?x-tunneled-method=PUT">
<!-- ... -->
</form>
Or override it via the C<X-HTTP-Method-Override> header in a request:
my $req = HTTP::Request->new(POST => '/foo', [
'X-HTTP-Method-Override' => 'PUT'
]);
=head1 Description
Writing
L<REST|http://en.wikipedia.org/wiki/Representational_State_Transfer>ful apps
is a good thing, but if you're also trying to support web browsers, it would
be nice not to be reduced to C<GET> and C<POST> for everything.
This middleware allows for C<POST> requests that pretend to be something else:
by adding either a header named C<X-HTTP-Method-Override> to the request, or
a query parameter named C<x-tunneled-method> to the URI, the client can say
what method it actually meant. That is, as long as it meant one of these:
=over
=item * GET
=item * POST
=item * HEAD
=item * PUT
=item * DELETE
=item * OPTIONS
=item * TRACE
=item * CONNECT
=item * PATCH
=back
If so, then the C<REQUEST_METHOD> in the PSGI environment will be replaced
with the client's desired value. The original request method is always stored
under the C<plack.original_request_method> key.
=head1 Configuration
These are the named arguments you can pass to C<new>. Or, more likely, on the
C<enable> line in your C<builder> block, as in
enable 'MethodOverride', header => 'X-HTTP-Method', param => 'my_method';
=head2 C<header>
Specifies the HTTP header name which specifies the overriding HTTP method.
Defaults to C<X-HTTP-Method-Override>, as used by Google for its APIs.
=head2 C<param>
Specifies the query parameter name to specify the overriding HTTP method.
Defaults to C<x-tunneled-method>.
=head1 Acknowledgements
This module gleefully steals from
L<Catalyst::TraitFor::Request::REST::ForBrowsers> by Dave Rolsky and the
original version by Tatsuhiko Miyagawa (which in turn stole from
L<HTTP::Engine::Middleware::MethodOverride>). Thanks to L<Aristotle
Pagaltzis|http://plasmasturm.org/> for the shove in this direction, to L<Matt
S Trout|http://www.trout.me.uk/> for suggesting that it be implemented as
middleware, and to L<Hans Dieter Pearcey|http://www.weftsoar.net/> for
convincing me not to parse body parameters.
=head1 Authors
=over 4
=item *
Tatsuhiko Miyagawa <miyagawa@bulknews.net>
=item *
David E. Wheeler <david@justatheory.com>
=item *
Aristotle Pagaltzis <pagaltzis@gmx.de>
=back
=head1 Copyright and License
This software is copyright (c) 2015 by Tatsuhiko Miyagawa, David E. Wheeler, Aristotle Pagaltzis.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
|