« QBit/WebInterface/Controller/Form/Field/button.pm
↑ Summary
QBit/WebInterface/Controller/Form.pm »
| 1 | package QBit::WebInterface::Request; | ||
| 2 | $QBit::WebInterface::Request::VERSION = '0.033'; | ||
| 3 | use qbit; | ||
| 4 | |||
| 5 | use base qw(QBit::Class); | ||
| 6 | |||
| 7 | use Exception::Request; | ||
| 8 | use Exception::Request::UnknownMethod; | ||
| 9 | |||
| 10 | __PACKAGE__->abstract_methods( | ||
| 11 | qw( | ||
| 12 | http_header | ||
| 13 | method | ||
| 14 | uri | ||
| 15 | scheme | ||
| 16 | server_name | ||
| 17 | server_port | ||
| 18 | remote_addr | ||
| 19 | query_string | ||
| 20 | _read_from_stdin | ||
| 21 | ) | ||
| 22 | ); | ||
| 23 | |||
| 24 | our $MAX_POST_REQEST_SIZE = 15 * 1024 * 1024; | ||
| 25 | |||
| 26 | sub param_names { | ||
| 27 | my ($self) = @_; | ||
| 28 | |||
| 29 | $self->_parse_params() unless exists($self->{'__PARAMS__'}); | ||
| 30 | |||
| 31 | return keys(%{$self->{'__PARAMS__'}}); | ||
| 32 | } | ||
| 33 | |||
| 34 | sub param_array { | ||
| 35 | my ($self, $name) = @_; | ||
| 36 | |||
| 37 | $self->_parse_params() unless exists($self->{'__PARAMS__'}); | ||
| 38 | |||
| 39 | return $self->{'__PARAMS__'}{$name || ''} || []; | ||
| 40 | } | ||
| 41 | |||
| 42 | sub param { | ||
| 43 | my ($self, $name, $default) = @_; | ||
| 44 | |||
| 45 | my $res = $self->param_array($name)->[0]; | ||
| 46 | return defined($res) ? $res : $default; | ||
| 47 | } | ||
| 48 | |||
| 49 | sub cookie { | ||
| 50 | my ($self, $name, $struc_type) = @_; | ||
| 51 | |||
| 52 | $self->_parse_cookies() unless exists($self->{'__COOKIES__'}); | ||
| 53 | |||
| 54 | return undef unless $self->{'__COOKIES__'}{$name}; | ||
| 55 | my $cookie = [@{$self->{'__COOKIES__'}{$name}}]; | ||
| 56 | |||
| 57 | if (ref($struc_type) eq 'ARRAY') { | ||
| 58 | return $cookie; | ||
| 59 | } elsif (ref($struc_type) eq 'HASH') { | ||
| 60 | push(@$cookie, undef) if @$cookie % 2; | ||
| 61 | return {@$cookie}; | ||
| 62 | } else { | ||
| 63 | return $cookie->[0]; | ||
| 64 | } | ||
| 65 | } | ||
| 66 | |||
| 67 | sub url { | ||
| 68 | my ($self, %opts) = @_; | ||
| 69 | |||
| 70 | my $url = $self->scheme() . '://'; | ||
| 71 | $url .= $self->server_name(); | ||
| 72 | $url .= ':' . $self->server_port() if !in_array($self->server_port(), [80, 443]); | ||
| 73 | $url .= $self->uri() unless $opts{'no_uri'}; | ||
| 74 | |||
| 75 | return $url; | ||
| 76 | } | ||
| 77 | |||
| 78 | sub _parse_params { | ||
| 79 | my ($self) = @_; | ||
| 80 | |||
| 81 | $self->{'__PARAMS__'} = {}; | ||
| 82 | |||
| 83 | my @pairs; | ||
| 84 | |||
| 85 | if ($self->method ne 'GET') { | ||
| 86 | my ($buffer, $tmp, $size) = ('', '', 0); | ||
| 87 | while (my $cnt = $self->_read_from_stdin(\$tmp, 1024 * 1024)) { | ||
| 88 | $size += $cnt; | ||
| 89 | throw gettext('Too big request') if $size > $MAX_POST_REQEST_SIZE; | ||
| 90 | $buffer .= $tmp; | ||
| 91 | } | ||
| 92 | |||
| 93 | if ($self->http_header('content-type') =~ /^multipart\/form\-data/) { | ||
| 94 | my ($spliter, $end, $data) = $buffer =~ m/^([^ \r\n]+)([\r\n]{1,2})(.*?)\2\1--.?.?$/s; | ||
| 95 | |||
| 96 | foreach my $block (split(/$end\Q$spliter\E$end/, $data)) { | ||
| 97 | my ($header, $content) = split($end . $end, $block, 2); | ||
| 98 | |||
| 99 | my %header; | ||
| 100 | |||
| 101 | foreach my $line (split(/(?:$end)|(?:\s*;\s*)/, $header)) { | ||
| 102 | my ($name, $value) = split(/=|:\s*/, $line, 2); | ||
| 103 | $value =~ s/^"?(.*?)"?$/$1/; | ||
| 104 | $header{$name} = $value; | ||
| 105 | } | ||
| 106 | |||
| 107 | if ($header{'filename'}) { | ||
| 108 | $self->_unescape(\$header{'filename'}); | ||
| 109 | for ($header{'filename'}) { | ||
| 110 | s/^"([^"]+)"$/$1/; | ||
| 111 | s/[\\\/]([^\\\/]+)$/$1/; | ||
| 112 | } | ||
| 113 | push(@pairs, [$header{'name'}, {filename => $header{'filename'}, content => $content}]); | ||
| 114 | } elsif ($header{'name'}) { | ||
| 115 | push(@pairs, [$header{'name'}, \$content]); | ||
| 116 | } | ||
| 117 | } | ||
| 118 | } elsif ($self->http_header('content-type') =~ /^application\/x\-www\-form\-urlencoded/) { | ||
| 119 | push(@pairs, map {[split('=', $_, 2)]} split('&', $buffer)); | ||
| 120 | } else { | ||
| 121 | push(@pairs, ['', \$buffer]); | ||
| 122 | } | ||
| 123 | } | ||
| 124 | |||
| 125 | push(@pairs, map {[split('=', $_, 2)]} split('&', $self->query_string)); | ||
| 126 | |||
| 127 | foreach (@pairs) { | ||
| 128 | my ($pname, $pvalue) = @$_; | ||
| 129 | |||
| 130 | next unless defined($pname); | ||
| 131 | |||
| 132 | $self->_unescape(\$pname); | ||
| 133 | $self->_unescape(\$pvalue) if defined($pvalue) && ref($pvalue) ne 'HASH'; | ||
| 134 | |||
| 135 | $self->{'__PARAMS__'}{$pname} = [] | ||
| 136 | unless exists($self->{'__PARAMS__'}{$pname}); | ||
| 137 | push(@{$self->{'__PARAMS__'}{$pname}}, $pvalue); | ||
| 138 | } | ||
| 139 | } | ||
| 140 | |||
| 141 | sub _parse_cookies { | ||
| 142 | my ($self) = @_; | ||
| 143 | |||
| 144 | $self->{'__COOKIES__'} = {}; | ||
| 145 | |||
| 146 | my $cookie_str = $self->http_header('Cookie'); | ||
| 147 | |||
| 148 | foreach (split('[;,] ?', $cookie_str)) { | ||
| 149 | s/\s*(.*?)\s*/$1/; | ||
| 150 | my ($key, $value) = split('=', $_, 2); | ||
| 151 | next unless defined($key); | ||
| 152 | |||
| 153 | my @values = defined($value) ? split(/[&;]/, $value) : (); | ||
| 154 | $self->_unescape(\$_) foreach @values; | ||
| 155 | |||
| 156 | $self->{'__COOKIES__'}{$key} ||= []; | ||
| 157 | push(@{$self->{'__COOKIES__'}{$key}}, @values); | ||
| 158 | } | ||
| 159 | } | ||
| 160 | |||
| 161 | sub _unescape { | ||
| 162 | my ($self, $str_ptr) = @_; | ||
| 163 | |||
| 164 | if (ref($$str_ptr) eq 'SCALAR') { | ||
| 165 | $$str_ptr = $$$str_ptr; | ||
| 166 | } else { | ||
| 167 | $$str_ptr =~ tr/+/ /; | ||
| 168 | $$str_ptr =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; | ||
| 169 | } | ||
| 170 | utf8::decode($$str_ptr); | ||
| 171 | } | ||
| 172 | |||
| 173 | TRUE; |