| 1 | package Apache::AuthCookie::Params::CGI; | ||
| 2 | $Apache::AuthCookie::Params::CGI::VERSION = '3.32'; | ||
| 3 | # ABSTRACT: Internal CGI Params Subclass | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | use Carp (); | ||
| 8 | use WWW::Form::UrlEncoded qw(parse_urlencoded); | ||
| 9 | use Hash::MultiValue; | ||
| 10 | use HTTP::Body; | ||
| 11 | use Encode (); | ||
| 12 | |||
| 13 | |||
| 14 | sub new { | ||
| 15 | my ($class, $r) = @_; | ||
| 16 | |||
| 17 | $class = ref $class || $class; | ||
| 18 | |||
| 19 | return bless { | ||
| 20 | _request => $r | ||
| 21 | }, $class; | ||
| 22 | } | ||
| 23 | |||
| 24 | |||
| 25 | sub request { | ||
| 26 | my $self = shift; | ||
| 27 | |||
| 28 | return $self->{_request}; | ||
| 29 | } | ||
| 30 | |||
| 31 | |||
| 32 | sub param { | ||
| 33 | my ($self, $name, @vals) = @_; | ||
| 34 | |||
| 35 | # no param name given, return list of all parameter names. | ||
| 36 | unless (defined $name) { | ||
| 37 | return $self->params->keys; | ||
| 38 | } | ||
| 39 | |||
| 40 | # set values | ||
| 41 | if (@vals) { | ||
| 42 | $self->params->remove($name); | ||
| 43 | $self->params->add($name => @vals); | ||
| 44 | } | ||
| 45 | |||
| 46 | # return values | ||
| 47 | return $self->params->get_all($name); | ||
| 48 | } | ||
| 49 | |||
| 50 | |||
| 51 | sub params { | ||
| 52 | my $self = shift; | ||
| 53 | |||
| 54 | $self->{_params} ||= Hash::MultiValue->new( | ||
| 55 | map $_->flatten, $self->query_params, $self->body_params | ||
| 56 | ); | ||
| 57 | } | ||
| 58 | |||
| 59 | |||
| 60 | sub query_params { | ||
| 61 | my $self = shift; | ||
| 62 | |||
| 63 | $self->{_query_params} ||= $self->_compute_pnote('request.query-params', sub { | ||
| 64 | my $query = $self->request->args || ''; | ||
| 65 | |||
| 66 | $self->_decode( Hash::MultiValue->new(parse_urlencoded($query)) ); | ||
| 67 | }); | ||
| 68 | } | ||
| 69 | |||
| 70 | |||
| 71 | sub body_params { | ||
| 72 | my $self = shift; | ||
| 73 | |||
| 74 | $self->{_body_params} ||= $self->_compute_pnote('request.body-params', sub { | ||
| 75 | $self->_decode( Hash::MultiValue->from_mixed($self->_http_body->param) ); | ||
| 76 | }); | ||
| 77 | } | ||
| 78 | |||
| 79 | |||
| 80 | sub content_length { | ||
| 81 | my $self = shift; | ||
| 82 | |||
| 83 | $self->{_content_length} ||= | ||
| 84 | $self->request->headers_in->get('Content-Length') || 0; | ||
| 85 | } | ||
| 86 | |||
| 87 | |||
| 88 | sub content_type { | ||
| 89 | my $self = shift; | ||
| 90 | |||
| 91 | $self->{_content_type} ||= | ||
| 92 | $self->request->headers_in->get('Content-Type') || ''; | ||
| 93 | } | ||
| 94 | |||
| 95 | sub _http_body { | ||
| 96 | my $self = shift; | ||
| 97 | |||
| 98 | $self->{_http_body} ||= $self->_compute_pnote('request.body', sub { | ||
| 99 | $self->_read_body; | ||
| 100 | }); | ||
| 101 | } | ||
| 102 | |||
| 103 | sub _read_body { | ||
| 104 | my $self = shift; | ||
| 105 | |||
| 106 | my $length = $self->content_length; | ||
| 107 | |||
| 108 | my $body = HTTP::Body->new($self->content_type, $length); | ||
| 109 | |||
| 110 | # HTTP::Body creates temp files for uploads. we need to tell it to clean up | ||
| 111 | # those files when the body goes out of scope. | ||
| 112 | $body->cleanup(1); | ||
| 113 | |||
| 114 | my $r = $self->request; | ||
| 115 | |||
| 116 | my $spin = 0; | ||
| 117 | |||
| 118 | while ($length) { | ||
| 119 | $r->read(my $buffer, ($length < 8192) ? $length : 8192); | ||
| 120 | |||
| 121 | my $bytes_read = length $buffer; | ||
| 122 | |||
| 123 | $length -= $bytes_read; | ||
| 124 | $body->add($buffer); | ||
| 125 | |||
| 126 | # guard against a signal interrupting read() | ||
| 127 | if ($bytes_read == 0 && $spin++ > 2000) { | ||
| 128 | Carp::croak "Bad Content-Length: maybe client disconnect? ($length bytes remaining)"; | ||
| 129 | } | ||
| 130 | } | ||
| 131 | |||
| 132 | return $body; | ||
| 133 | } | ||
| 134 | |||
| 135 | # utility method to fetch a pnote, or set it to a computed value if it has not | ||
| 136 | # already been set. | ||
| 137 | sub _compute_pnote { | ||
| 138 | my ($self, $key, $code) = @_; | ||
| 139 | |||
| 140 | my $r = $self->request; | ||
| 141 | |||
| 142 | unless (defined $r->pnotes($key)) { | ||
| 143 | $r->pnotes($key, $code->()); | ||
| 144 | } | ||
| 145 | |||
| 146 | return $r->pnotes($key); | ||
| 147 | } | ||
| 148 | |||
| 149 | sub _decode { | ||
| 150 | my ($self, $hash) = @_; | ||
| 151 | |||
| 152 | my $r = $self->request; | ||
| 153 | my $auth_name = $r->auth_name; | ||
| 154 | |||
| 155 | if (my $encoding = $r->dir_config("${auth_name}Encoding")) { | ||
| 156 | my $decoded = Hash::MultiValue->new; | ||
| 157 | |||
| 158 | $hash->each(sub { | ||
| 159 | my @dec = map { Encode::decode($encoding, $_) } @_; | ||
| 160 | |||
| 161 | $decoded->add(@dec); | ||
| 162 | }); | ||
| 163 | |||
| 164 | return $decoded; | ||
| 165 | } | ||
| 166 | else { | ||
| 167 | return $hash; | ||
| 168 | } | ||
| 169 | } | ||
| 170 | |||
| 171 | 1; | ||
| 172 | |||
| 173 | __END__ |