Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merged pull request #2 from lwsitu/master.

redirect after a paste with a HTTP redirect instead of a meta redirect
  • Loading branch information...
commit 9bf939b819dc3701e4c83e9197e6e7fa21474759 2 parents 3defdf3 + 6306ece
@rcaputo authored
View
3  lib/Bot/Pastebot/Administrivia.pm
@@ -41,7 +41,7 @@ sub get_pid {
my $name = _get_name();
my %conf = get_items_by_name($name);
my $pidfile = $conf{pidfile};
- return unless -e $pidfile;
+ return unless $pidfile && -e $pidfile;
my $pid = do {
local $/;
open my $fh, '<', $pidfile or die "open($pidfile): $!";
@@ -56,6 +56,7 @@ sub write_pidfile {
my $name = _get_name();
my %conf = get_items_by_name($name);
my $pidfile = $conf{pidfile};
+ return unless $pidfile;
open my $fh, '>', $pidfile or die "open($pidfile): $!";
print $fh $$;
close $fh;
View
35 lib/Bot/Pastebot/Server/Http.pm
@@ -17,7 +17,7 @@ use File::ShareDir qw(dist_dir);
use Bot::Pastebot::Conf qw( get_names_by_type get_items_by_name );
use Bot::Pastebot::WebUtil qw(
static_response parse_content parse_cookie dump_content html_encode
- is_true cookie
+ is_true cookie redirect
);
use Bot::Pastebot::Data qw( channels store_paste fetch_paste is_ignored );
@@ -324,18 +324,27 @@ sub httpd_session_got_query {
$paste = fix_paste($paste, 0, 0, 0, 0);
- my $response = static_response(
- $heap->{my_template},
- "$heap->{my_static}/paste-answer.html",
- { paste_id => $id,
- error => $error,
- paste_link => $paste_link,
- nick => $nick,
- summary => $summary,
- paste => $paste,
- footer => PAGE_FOOTER,
- }
- );
+ my $response;
+
+ if( $error ) {
+ $response = static_response(
+ $heap->{my_template},
+ "$heap->{my_static}/paste-error.html",
+ {
+ error => $error,
+ footer => PAGE_FOOTER,
+ }
+ );
+ } else {
+ $response = redirect(
+ $heap->{my_template},
+ "$heap->{my_static}/paste-answer.html",
+ {
+ paste_id => $id,
+ paste_link => $paste_link,
+ },
+ );
+ }
if ($channel and $channel =~ /^\#/) {
$kernel->post(
View
54 lib/Bot/Pastebot/WebUtil.pm
@@ -15,7 +15,7 @@ use base qw(Exporter);
our @EXPORT_OK = qw(
url_decode url_encode parse_content parse_cookie static_response
dump_content dump_query_as_response base64_decode html_encode
- is_true cookie
+ is_true cookie redirect
);
#------------------------------------------------------------------------------
@@ -130,36 +130,47 @@ sub parse_cookie {
return { map url_decode($_), map /([^=]+)=?(.*)/s, split /; ?/, $cookie };
}
-# Generate a static response from a file.
-sub static_response {
+sub _render_template {
my ($template, $filename, $record) = @_;
- my ($code, $content);
+ my ($content, $error);
if (open(my $template_fh, "<", $filename)) {
- $code = 200;
+
$content = eval { $template->process($template_fh, $record) };
if ($@ || !defined $content || !length $content) {
- my $error = $template->error || 'unknown error';
- $code = 500;
+ my $template_error = $template->error || 'unknown error';
+ $error = 1;
$content = (
"<html><head><title>Template Error</title></head>" .
- "<body>Error processing $filename: $error</body></html>"
+ "<body>Error processing $filename: $template_error</body></html>"
);
}
- }
- else {
- $code = 500;
+ } else {
+ $error = 1;
$content = (
"<html><head><title>Template Error</title></head>" .
"<body>Error opening $filename: $!</body></html>"
);
}
+ return +{
+ content => $content,
+ error => 1,
+ };
+}
+
+# Generate a static response from a file.
+sub static_response {
+ my ($template, $filename, $record) = @_;
+
+ my $code = 200;
+ my $result = _render_template( $template, $filename, $record );
+ $code = 500 if $result->{error};
- my $response = new HTTP::Response($code);
+ my $response = HTTP::Response->new($code);
$response->push_header('Content-type', 'text/html');
- $response->content($content);
+ $response->content( $result->{content} );
if (wantarray()) {
return(1, $response);
@@ -167,6 +178,23 @@ sub static_response {
return $response;
}
+# redirect to a paste
+sub redirect {
+ my ($template, $filename, $record, $response_code) = @_;
+
+ my $response = HTTP::Response->new( $response_code || 303 );
+ my $paste_link = $record->{paste_link};
+ $response->push_header( "Location", $paste_link );
+
+ my $result = _render_template( $template, $filename, $record );
+ unless( $result->{error} ) {
+ $response->push_header( "Content-type", "text/html" );
+ $response->content( $result->{content} );
+ }
+
+ return $response;
+}
+
# Dump a query's content as a table.
sub dump_content {
my $content = shift;
View
16 static/paste-answer.html
@@ -3,25 +3,11 @@
<head>
<title>Your paste, number [% $paste_id %]...</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <meta http-equiv="refresh" content="30;url=[% $paste_link %]">
</head>
<body>
- [% $error %]
<p>
This content is stored as
- <a href='[% $paste_link %]'>[% $paste_link %]</a>. You will be redirected
- shortly.
- </p>
- <p>
- From: [% $nick %]
- <br>
- Summary: [% $summary %]
- </p>
- <p>
- [% $paste %]
- </p>
- <p>
- [% $footer %]
+ <a href='[% $paste_link %]'>[% $paste_link %]</a>.
</p>
</body>
</html>
View
19 static/paste-error.html
@@ -0,0 +1,19 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
+<html>
+ <head>
+ <title>Paste error</title>
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
+ <style type="text/css">
+ .error {
+ margin-left: 2em;
+ border: 1px solid gray;
+ }
+ </style>
+ </head>
+ <body>
+ Error:
+ <div class="error">
+ [% $error %]
+ </div>
+ </body>
+</html>
Please sign in to comment.
Something went wrong with that request. Please try again.