fu: Close-up of Fu, bringing a scoop of water to her mouth (Default)
fu ([personal profile] fu) wrote in [site community profile] changelog2010-07-05 03:24 pm

[dw-free] Move /interface/s2 into DW::Routing

[commit: http://hg.dwscoalition.org/dw-free/rev/e49268dcb070]

http://bugs.dwscoalition.org/show_bug.cgi?id=2289

Convert /interface/s2 to use Template Toolkit. Add methods for the HTTP
statuses. Reorder the paths so that DW::Controller gets checked before the
old /interface handler.

Patch by [personal profile] fu.

Files modified:
  • cgi-bin/Apache/LiveJournal.pm
  • cgi-bin/Apache/LiveJournal/Interface/S2.pm
  • cgi-bin/DW/Request/Apache2.pm
  • cgi-bin/DW/Routing.pm
--------------------------------------------------------------------------------
diff -r 6cd7c120dd96 -r e49268dcb070 cgi-bin/Apache/LiveJournal.pm
--- a/cgi-bin/Apache/LiveJournal.pm	Mon Jul 05 23:22:24 2010 +0800
+++ b/cgi-bin/Apache/LiveJournal.pm	Mon Jul 05 23:30:43 2010 +0800
@@ -27,7 +27,6 @@ use LJ::Blob;
 use LJ::Blob;
 use Apache::LiveJournal::Interface::Blogger;
 use Apache::LiveJournal::Interface::AtomAPI;
-use Apache::LiveJournal::Interface::S2;
 use Apache::LiveJournal::Interface::ElsewhereInfo;
 use Apache::LiveJournal::PalImg;
 use LJ::ModuleCheck;
@@ -965,6 +964,11 @@ sub trans
         return $inthandle if defined $inthandle;
     }
 
+    # see if there is a modular handler for this URI
+    my $ret = LJ::URI->handle( $uri, $r );
+    $ret = DW::Routing->call unless defined $ret;
+    return $ret if defined $ret;
+
     # protocol support
     if ($uri =~ m!^/(?:interface/(\w+))|cgi-bin/log\.cgi!) {
         my $int = $1 || "flat";
@@ -975,18 +979,8 @@ sub trans
             $r->push_handlers(PerlResponseHandler => \&interface_content);
             return OK;
         }
-        if ($int eq "s2") {
-            Apache::LiveJournal::Interface::S2->load;
-            $r->push_handlers(PerlResponseHandler => \&Apache::LiveJournal::Interface::S2::handler);
-            return OK;
-        }
         return 404;
     }
-
-    # see if there is a modular handler for this URI
-    my $ret = LJ::URI->handle($uri, $r);
-    $ret = DW::Routing->call unless defined $ret;
-    return $ret if defined $ret;
 
     # customview (get an S1 journal by number)
     if ($uri =~ m!^/customview\.cgi!) {
diff -r 6cd7c120dd96 -r e49268dcb070 cgi-bin/Apache/LiveJournal/Interface/S2.pm
--- a/cgi-bin/Apache/LiveJournal/Interface/S2.pm	Mon Jul 05 23:22:24 2010 +0800
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,141 +0,0 @@
-#!/usr/bin/perl
-#
-# This code was forked from the LiveJournal project owned and operated
-# by Live Journal, Inc. The code has been modified and expanded by
-# Dreamwidth Studios, LLC. These files were originally licensed under
-# the terms of the license supplied by Live Journal, Inc, which can
-# currently be found at:
-#
-# http://code.livejournal.org/trac/livejournal/browser/trunk/LICENSE-LiveJournal.txt
-#
-# In accordance with the original license, this code and all its
-# modifications are provided under the GNU General Public License.
-# A copy of that license can be found in the LICENSE file included as
-# part of this distribution.
-
-package Apache::LiveJournal::Interface::S2;
-
-use strict;
-use MIME::Base64 ();
-use Apache2::Const -compile => qw/OK NOT_FOUND/;
-
-sub load { 1 }
-
-sub handler {
-    my $r = DW::Request->get;
-
-    my $meth = $r->method;
-    my %GET = $r->query_string;
-    my $uri = $r->uri;
-    my $id;
-    if ($uri =~ m!^/interface/s2/(\d+)$!) {
-        $id = $1 + 0;
-    } else {
-        return Apache2::Const::NOT_FOUND;
-    }
-
-    my $lay = LJ::S2::load_layer($id);
-    return error($r, 404, 'Layer not found', "There is no layer with id $id at this site")
-        unless $lay;
-
-    LJ::auth_digest($r);
-    my $u = LJ::get_remote();
-    unless ($u) {
-        # Tell the client how it can authenticate
-        # use digest authorization.
-
-        $r->content_type("text/plain; charset=utf-8");
-        $r->print("Unauthorized\nYou must send your $LJ::SITENAME username and password or a valid session cookie\n");
-
-        return Apache2::Const::OK;
-    }
-
-    my $dbr = LJ::get_db_reader();
-
-    my $lu = LJ::load_userid($lay->{'userid'});
-
-    return error($r, 500, "Error", "Unable to find layer owner.")
-        unless $lu;
-
-    if ($meth eq 'GET') {
-
-        return error($r, 403, "Forbidden", "You are not authorized to retrieve this layer")
-            unless $lu->user eq 'system' || $u->can_manage( $lu );
-
-        my $layerinfo = {};
-        LJ::S2::load_layer_info($layerinfo, [ $id ]);
-        my $srcview = exists $layerinfo->{$id}->{'source_viewable'} ?
-            $layerinfo->{$id}->{'source_viewable'} : 1;
-
-        # Disallow retrieval of protected system layers
-        return error($r, 403, "Forbidden", "The requested layer is restricted")
-            if $lu->{'user'} eq 'system' && ! $srcview;
-
-        my $s2code = LJ::S2::load_layer_source($id);
-
-        $r->content_type("application/x-danga-s2-layer");
-        $r->print($s2code);
-
-        return Apache2::Const::OK;
-    }
-    elsif ($meth eq 'PUT') {
-
-        return error($r, 403, "Forbidden", "You are not authorized to edit this layer")
-            unless $u->can_manage( $lu );
-
-        return error($r, 403, "Forbidden", "Your account type is not allowed to edit layers")
-            unless $u->can_create_s2_styles;
-
-        # Read in the entity body to get the source
-        my $len = $r->header_in("Content-length")+0;
-
-        return error($r, 400, "Bad Request", "Supply S2 layer code in the request entity body and set Content-length")
-            unless $len;
-
-        return error($r, 415, "Bad Media Type", "Request body must be of type application/x-danga-s2-layer")
-            unless lc($r->header_in("Content-type")) eq 'application/x-danga-s2-layer';
-
-        my $s2code;
-        $r->read($s2code, $len);
-
-        my $error = "";
-        LJ::S2::layer_compile($lay, \$error, { 's2ref' => \$s2code });
-
-        if ($error) {
-            error($r, 500, "Layer Compile Error", "An error was encountered while compiling the layer.");
-
-            ## Strip any absolute paths
-            $error =~ s/LJ::.+//s;
-            $error =~ s!, .+?(src/s2|cgi-bin)/!, !g;
-
-            $r->print($error);
-            return Apache2::Const::OK;
-        }
-        else {
-            $r->status_line("201 Compiled and Saved");
-            $r->header_out("Location" => "$LJ::SITEROOT/interface/s2/$id");
-            $r->content_type("text/plain; charset=utf-8");
-            $r->print("Compiled and Saved\nThe layer was uploaded successfully.\n");
-
-            return Apache2::Const::OK;
-        }
-    }
-    else {
-        #  Return 'method not allowed' so that we can add methods in future
-        # and clients will get a sensible error from old servers.
-        return error($r, 405, 'Method Not Allowed', 'Only GET and PUT are supported for this resource');
-    }
-}
-
-sub error {
-    my ($r, $code, $string, $long) = @_;
-
-    $r->status_line("$code $string");
-    $r->content_type("text/plain; charset=utf-8");
-    $r->print("$string\n$long\n");
-
-    # Tell Apache OK so it won't try to handle the error
-    return Apache2::Const::OK;
-}
-
-1;
diff -r 6cd7c120dd96 -r e49268dcb070 cgi-bin/DW/Request/Apache2.pm
--- a/cgi-bin/DW/Request/Apache2.pm	Mon Jul 05 23:22:24 2010 +0800
+++ b/cgi-bin/DW/Request/Apache2.pm	Mon Jul 05 23:30:43 2010 +0800
@@ -20,7 +20,7 @@ use DW::Request::Base;
 use DW::Request::Base;
 use base 'DW::Request::Base';
 
-use Apache2::Const -compile => qw/ :common REDIRECT HTTP_NOT_MODIFIED /;
+use Apache2::Const -compile => qw/ :common :http /;
 use Apache2::Log ();
 use Apache2::Request;
 use Apache2::Response ();
@@ -284,6 +284,34 @@ sub NOT_FOUND {
     return Apache2::Const::NOT_FOUND;
 }
 
+sub SERVER_ERROR {
+    return Apache2::Const::SERVER_ERROR;
+}
+
+sub HTTP_UNAUTHORIZED {
+    return Apache2::Const::HTTP_UNAUTHORIZED;
+}
+
+sub HTTP_BAD_REQUEST {
+    return Apache2::Const::HTTP_BAD_REQUEST;
+}
+
+sub HTTP_UNSUPPORTED_MEDIA_TYPE {
+    return Apache2::Const::HTTP_UNSUPPORTED_MEDIA_TYPE;
+}
+
+sub HTTP_INTERNAL_SERVER_ERROR {
+    return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
+}
+
+sub HTTP_METHOD_NOT_ALLOWED {
+    return Apache2::Const::HTTP_METHOD_NOT_ALLOWED;
+}
+
+sub FORBIDDEN {
+    return Apache2::Const::FORBIDDEN;
+}
+
 # spawn a process for an external program
 sub spawn {
     my DW::Request::Apache2 $self = shift;
diff -r 6cd7c120dd96 -r e49268dcb070 cgi-bin/DW/Routing.pm
--- a/cgi-bin/DW/Routing.pm	Mon Jul 05 23:22:24 2010 +0800
+++ b/cgi-bin/DW/Routing.pm	Mon Jul 05 23:30:43 2010 +0800
@@ -34,9 +34,11 @@ my $default_content_types = {
 my $default_content_types = {
     'html' => "text/html; charset=utf-8",
     'json' => "application/json; charset=utf-8",
+    'plain' => "text/plain; charset=utf-8",
 };
 
 LJ::ModuleLoader->require_subclasses( "DW::Controller" );
+LJ::ModuleLoader->require_subclasses( "DW::Controller::Interface" );
 
 =head1 NAME
 
--------------------------------------------------------------------------------

Post a comment in response:

This account has disabled anonymous posting.
If you don't have an account you can create one now.
HTML doesn't work in the subject.
More info about formatting

If you are unable to use this captcha for any reason, please contact us by email at support@dreamwidth.org