1 package AnyEvent
::HTTPD
::Request
;
7 AnyEvent::HTTPD::Request - A web application request handle for L<AnyEvent::HTTPD>
11 This is the request object as generated by L<AnyEvent::HTTPD> and given
12 in the request callbacks.
22 my $class = ref($this) || $this;
29 This method returns the URL of the current request.
35 my $url = $self->{url
};
36 my $u = URI
->new ($url);
43 defined $self->form_id
48 my $id = $self->parm ("_APP_SRV_FORM_ID");
49 $id = $self->parm ("_afid") if defined $self->parm ("_afid");
53 =item B<form ($content, $callback)>
55 This method will create a form for you and bind it to the C<$handler>
56 you gave. The content of the form tag can be given by C<$content>, which
57 can either be a string or a code reference, which will be called and should
58 return the form content.
60 When the form is submitted the C<$callback> will be called before the submit
61 request executes any of your content callbacks. The form ID is transmitted via
62 a hidden input element with the name C<_APP_SRV_FORM_ID>, and you should take
63 care not to use that form element name yourself.
65 The C<$callback> will receive as first argument the L<AnyEvent::HTTPD> object.
67 You can access the transmitted form parameters via the C<parm> method.
72 my ($self, $cont, $cb) = @_;
73 my $id = $self->{httpd
}->alloc_id ($cb);
75 '<form action="'.$url.'" method="POST" enctype="multipart/form-data">'
76 .'<input type="hidden" name="_APP_SRV_FORM_ID" value="'.$id.'" />'
77 .(ref $cont ?
$cont->() : $cont)
81 =item B<respond ([$res])>
83 This method will send a response to the request.
84 If no C<$res> argument was given eventually accumulated output will be
87 Otherwise C<$res> can be:
91 =item * an array reference
93 Then the array reference has these elements:
95 my ($code, $message, $header_hash, $content) =
96 [200, 'ok', { 'Content-Type' => 'text/html' }, '<h1>Test</h1>' }]
98 =item * a hash reference
100 If it was a hash reference the hash is first searched for the C<redirect>
101 key and if that key does not exist for the C<content> key.
103 The value for the C<redirect> key should contain the URL that you want to redirect
106 The value for the C<content> key should contain an array reference with the first
107 value being the content type and the second the content.
114 '/image/elmex' => sub {
115 my ($httpd, $req) = @_;
117 open IMG, "$ENV{HOME}/media/images/elmex.png"
119 [404, 'not found', { 'Content-Type' => 'text/plain' }, 'not found']
122 $req->respond ({ content => ['image/png', do { local $/; <IMG> }] });
129 my ($self, $res) = @_;
131 my $rescb = $self->{resp
};
133 if (ref $res eq 'HASH') {
135 if ($h->{redirect
}) {
137 301, 'redirected', { Location
=> $h->{redirect
} },
138 "Redirected to <a href=\"$h->{redirect}\">here</a>"
140 } elsif ($h->{content
}) {
142 200, 'ok', { 'Content-Type' => $h->{content
}->[0] },
148 if (not defined $res) {
149 if ($self->{output
} eq '') {
150 $rescb->(404, "ok", { 'Content-Type' => 'text/html' }, "<h1>No content</h1>");
152 $rescb->(200, "ok", { 'Content-Type' => 'text/html' }, $self->{output
});
159 =item B<link ($label, $callback, $newurl)>
161 This method returns a html link which will call C<$callback>
162 when the user follows the link. It uses the C<_afid> param name,
163 so take care not to use it for other things.
164 C<$newurl> should be undef or the new (local) destination url,
165 see also the C<url> method above.
170 my ($self, $lbl, $cb, $newurl) = @_;
171 my $id = $self->{httpd
}->alloc_id ($cb);
172 unless (defined $newurl) { $newurl = $self->url; }
173 '<a href="'.$newurl.'?_afid='.$id.'">'.$lbl.'</a>';
178 Returns the first value of the form parameter C<$key> or undef.
183 my ($self, $key) = @_;
184 if (exists $self->{parm
}->{$key}) {
185 return $self->{parm
}->{$key}->[0]->[0]
192 Returns the request content or undef if only parameters for a form
199 return $self->{content
};
204 This method appends C<$str> to the response output of this request.
205 The accumulated output can be sent back as 'text/html' by calling the
206 C<respond> method without an argument.
210 sub o
{ shift->{output
} .= join '', @_ }
214 =head1 COPYRIGHT & LICENSE
216 Copyright 2008 Robin Redeker, all rights reserved.
218 This program is free software; you can redistribute it and/or modify it
219 under the same terms as Perl itself.