1 package BS
::HTTPD
::Request
;
8 BS::HTTPD::Request - A web application request handle for L<BS::HTTPD>
14 my $class = ref($this) || $this;
21 This method returns the URL of the current request.
27 my $url = $self->{cur_url
};
28 my $u = URI
->new ($url);
35 defined $self->form_id
40 my $id = $self->parm ("_APP_SRV_FORM_ID");
41 $id = $self->parm ("a") if defined $self->parm ("a");
45 =item B<form ($content, $callback)>
47 This method will create a form for you and bind it to the C<$handler>
48 you gave. The content of the form tag can be given by C<$content>, which
49 can either be a string or a code reference, which will be called and should
50 return the form content.
52 When the form is submitted the C<$callback> will be called before the submit
53 request executes any of your content callbacks. The form ID is transmitted via
54 a hidden input element with the name C<_APP_SRV_FORM_ID>, and you should take
55 care not to use that form element name yourself.
57 The C<$callback> will receive as first argument the L<BS::HTTPD> object.
59 You can access the transmitted form parameters via the C<parm> method.
64 my ($self, $cont, $cb) = @_;
65 my $id = $self->{httpd
}->alloc_id ($cb);
67 '<form action="'.$url.'" method="POST" enctype="multipart/form-data">'
68 .'<input type="hidden" name="_APP_SRV_FORM_ID" value="'.$id.'" />'
69 .(ref $cont ?
$cont->() : $cont)
73 =item B<respond ([$res])>
75 This method will send a response to the request.
76 If no C<$res> argument was given eventually accumulated output will be
79 Otherweis C<$res> can be:
83 =item * an array reference
85 Then the array reference has these elements:
87 my ($code, $message, $header_hash, $content) =
88 [200, 'ok', { 'Content-Type' => 'text/html' }, '<h1>Test</h1>' }]
90 =item * a hash reference
92 If it was a hash reference the hash is first searched for the C<redirect>
93 key and if that key does not exist for the C<content> key.
95 The value for the C<redirect> key should contain the URL that you want to redirect
98 The value for the C<content> key should contain an array reference with the first
99 value being the content type and the second the content.
106 _image_elmex => sub {
107 my ($httpd, $req) = @_;
109 open IMG, "$ENV{HOME}/media/images/elmex.png"
111 [404, 'not found', { 'Content-Type' => 'text/plain' }, 'not found']
114 $req->respond ({ content => ['image/png', do { local $/; <IMG> }] });
121 my ($self, $res) = @_;
123 my $rescb = $self->{resp
};
125 if (ref $res eq 'HASH') {
127 if ($h->{redirect
}) {
129 301, 'redirected', { Location
=> $h->{redirect
} },
130 "Redirected to <a href=\"$h->{redirect}\">here</a>"
132 } elsif ($h->{content
}) {
134 200, 'ok', { 'Content-Type' => $h->{content
}->[0] },
140 if (not defined $res) {
141 if ($self->{output
} eq '') {
142 $rescb->(404, "ok", { 'Content-Type' => 'text/html' }, "<h1>No content</h1>");
144 $rescb->(200, "ok", { 'Content-Type' => 'text/html' }, $self->{output
});
152 my ($self, $lbl, $cb, $newurl) = @_;
153 my $id = $self->{httpd
}->alloc_id ($cb);
154 $newurl //= $self->url;
155 '<a href="'.$newurl.'?a='.$id.'">'.$lbl.'</a>';
159 my ($self, $key) = @_;
160 if (exists $self->{parm
}->{$key}) {
161 return $self->{parm
}->{$key}->[0]->[0]
168 return $self->{content
};
171 sub o
{ shift->{output
} .= join '', @_ }