fu: Close-up of Fu, bringing a scoop of water to her mouth (Default)
fu ([personal profile] fu) wrote in [site community profile] changelog2011-02-25 12:41 pm

[dw-free] some AJAX endpoints can return invalid JSON

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

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

Use JSON::objToJson when we want JSON, not LJ::js_dumper (which is for
javascript variables). Needed so that libraries which are strict about JSON
(such as jQuery) won't have any trouble.

Patch by [personal profile] fu.

Files modified:
  • cgi-bin/weblib.pl
  • htdocs/tools/endpoints/cprod.bml
  • htdocs/tools/endpoints/ctxpopup.bml
  • htdocs/tools/endpoints/directorysearch.bml
  • htdocs/tools/endpoints/draft.bml
  • htdocs/tools/endpoints/extacct_auth.bml
  • htdocs/tools/endpoints/jobstatus.bml
  • htdocs/tools/endpoints/ljuser.bml
  • htdocs/tools/endpoints/multisearch.bml
  • htdocs/tools/endpoints/poll.bml
  • htdocs/tools/endpoints/pollgearman.bml
  • htdocs/tools/endpoints/pollvote.bml
  • htdocs/tools/endpoints/widget.bml
--------------------------------------------------------------------------------
diff -r f4d51a6cd107 -r ea69e4ab1d4e cgi-bin/weblib.pl
--- a/cgi-bin/weblib.pl	Thu Feb 24 21:56:24 2011 +0800
+++ b/cgi-bin/weblib.pl	Fri Feb 25 20:41:07 2011 +0800
@@ -2384,6 +2384,9 @@ sub no_access_error {
 }
 
 # Data::Dumper for JavaScript
+# use this only when printing out on a page as a JS variable
+# do not use for JSON requests -- it is not guaranteed to return
+# valid JSON
 sub js_dumper {
     my $obj = shift;
     if (ref $obj eq "HASH") {
diff -r f4d51a6cd107 -r ea69e4ab1d4e htdocs/tools/endpoints/cprod.bml
--- a/htdocs/tools/endpoints/cprod.bml	Thu Feb 24 21:56:24 2011 +0800
+++ b/htdocs/tools/endpoints/cprod.bml	Fri Feb 25 20:41:07 2011 +0800
@@ -16,6 +16,7 @@ _c?>
 {
     use strict;
     use vars qw(%GET);
+    use JSON;
 
     my $err = sub {
         my $msg = shift;
@@ -52,6 +53,6 @@ _c?>
     BML::set_content_type('text/javascript; charset=utf-8');
     BML::finish();
 
-    return LJ::js_dumper(\%ret);
+    return JSON::objToJson(\%ret);
 }
 _code?>
diff -r f4d51a6cd107 -r ea69e4ab1d4e htdocs/tools/endpoints/ctxpopup.bml
--- a/htdocs/tools/endpoints/ctxpopup.bml	Thu Feb 24 21:56:24 2011 +0800
+++ b/htdocs/tools/endpoints/ctxpopup.bml	Fri Feb 25 20:41:07 2011 +0800
@@ -143,6 +143,6 @@ _c?>
     $ret{is_banned} = $remote->has_banned( $u ) ? 1 : 0 if $remote;
 
     $ret{success} = 1;
-    return LJ::js_dumper(\%ret);
+    return JSON::objToJson( \%ret );
 }
 _code?>
diff -r f4d51a6cd107 -r ea69e4ab1d4e htdocs/tools/endpoints/directorysearch.bml
--- a/htdocs/tools/endpoints/directorysearch.bml	Thu Feb 24 21:56:24 2011 +0800
+++ b/htdocs/tools/endpoints/directorysearch.bml	Fri Feb 25 20:41:07 2011 +0800
@@ -117,6 +117,6 @@ _c?>
 
     sleep(1) if $LJ::IS_DEV_SERVER;
 
-    return LJ::js_dumper(\%ret);
+    return JSON::objToJson( \%ret );
 }
 _code?>
diff -r f4d51a6cd107 -r ea69e4ab1d4e htdocs/tools/endpoints/draft.bml
--- a/htdocs/tools/endpoints/draft.bml	Thu Feb 24 21:56:24 2011 +0800
+++ b/htdocs/tools/endpoints/draft.bml	Fri Feb 25 20:41:07 2011 +0800
@@ -17,10 +17,11 @@ _c?>
     use strict;
     use vars qw(%POST);
     use Storable;
+    use JSON;
 
     my $err = sub {
         my $msg = shift;
-        return LJ::js_dumper({
+        return JSON::objToJson({
             'alert' => $msg,
         });
     };
@@ -45,7 +46,7 @@ _c?>
         BML::set_content_type('text/javascript; charset=utf-8');
         BML::finish();
 
-        return LJ::js_dumper( $rv );
+        return JSON::objToJson( $rv );
     }
         
 
@@ -122,6 +123,6 @@ _c?>
     BML::set_content_type('text/javascript; charset=utf-8');
     BML::finish();
 
-    return LJ::js_dumper($ret);
+    return JSON::objToJson( $ret );
 }
 _code?>
diff -r f4d51a6cd107 -r ea69e4ab1d4e htdocs/tools/endpoints/extacct_auth.bml
--- a/htdocs/tools/endpoints/extacct_auth.bml	Thu Feb 24 21:56:24 2011 +0800
+++ b/htdocs/tools/endpoints/extacct_auth.bml	Fri Feb 25 20:41:07 2011 +0800
@@ -48,6 +48,6 @@ _c?>
     return $err->(BML::ml('/tools/endpoints/extacct_auth.bml.error.authfailed', { account => $account->displayname } )) unless $challenge;
 
     # return the challenge
-    return LJ::js_dumper( { challenge => $challenge, success => 1 } );
+    return JSON::objToJson( { challenge => $challenge, success => 1 } );
 }
 _code?>
diff -r f4d51a6cd107 -r ea69e4ab1d4e htdocs/tools/endpoints/jobstatus.bml
--- a/htdocs/tools/endpoints/jobstatus.bml	Thu Feb 24 21:56:24 2011 +0800
+++ b/htdocs/tools/endpoints/jobstatus.bml	Fri Feb 25 20:41:07 2011 +0800
@@ -56,6 +56,6 @@ _c?>
 
     sleep(2) if $LJ::IS_DEV_SERVER;
 
-    return LJ::js_dumper($ret);
+    return JSON::objToJson( $ret );
 }
 _code?>
diff -r f4d51a6cd107 -r ea69e4ab1d4e htdocs/tools/endpoints/ljuser.bml
--- a/htdocs/tools/endpoints/ljuser.bml	Thu Feb 24 21:56:24 2011 +0800
+++ b/htdocs/tools/endpoints/ljuser.bml	Fri Feb 25 20:41:07 2011 +0800
@@ -64,6 +64,6 @@ _c?>
 
     $ret{success} = 1;
 
-    return LJ::js_dumper(\%ret);
+    return JSON::objToJson( \%ret );
 }
 _code?>
diff -r f4d51a6cd107 -r ea69e4ab1d4e htdocs/tools/endpoints/multisearch.bml
--- a/htdocs/tools/endpoints/multisearch.bml	Thu Feb 24 21:56:24 2011 +0800
+++ b/htdocs/tools/endpoints/multisearch.bml	Fri Feb 25 20:41:07 2011 +0800
@@ -28,7 +28,7 @@ _c?>
     BML::finish();
     BML::noparse();
 
-    return JSON::objToJson({error => "There is no any query provided.",}) unless $q;
+    return JSON::objToJson({error => "There is no query provided.",}) unless $q;
 
     if ($type eq "user" || $type eq "ljtalk") {
         my $user = lc($q);
@@ -60,7 +60,7 @@ _c?>
             my %ret;
             $ret{status} = 'success';
             $ret{result} = '<a href="' . $url . '">' . $user . '</a>';
-            return LJ::js_dumper(\%ret);
+            return JSON::objToJson( \%ret );
         } else {
             return JSON::objToJson({error => "There is no such user.",});
         }
@@ -101,7 +101,7 @@ _c?>
                 $ret{result} .= '&nbsp';
             }
         }
-        return LJ::js_dumper(\%ret) if $ret{result};
+        return JSON::objToJson( \%ret ) if $ret{result};
 
         return JSON::objToJson({error => "No user found",});
     }
@@ -123,7 +123,7 @@ _c?>
             } else {
                 $ret{result} = '<a href="' . $u->profile_url . '">' . $u->{user}. '</a>';
             }
-            return LJ::js_dumper(\%ret);
+            return JSON::objToJson( \%ret );
         } elsif (@uids > 1) {
             my $us = LJ::load_userids(@uids);
 
@@ -134,7 +134,7 @@ _c?>
                                                     timesort   => 1,
                                                     perpage    => 50,
                                                   );
-            return LJ::js_dumper(\%ret);
+            return JSON::objToJson( \%ret );
         }
 
         # If we haven't returned already then the search turned up nothing.
diff -r f4d51a6cd107 -r ea69e4ab1d4e htdocs/tools/endpoints/poll.bml
--- a/htdocs/tools/endpoints/poll.bml	Thu Feb 24 21:56:24 2011 +0800
+++ b/htdocs/tools/endpoints/poll.bml	Fri Feb 25 20:41:07 2011 +0800
@@ -63,6 +63,6 @@ _c?>
 
     sleep(1.5) if $LJ::IS_DEV_SERVER;
 
-    return LJ::js_dumper($ret);
+    return JSON::objToJson( $ret );
 }
 _code?>
diff -r f4d51a6cd107 -r ea69e4ab1d4e htdocs/tools/endpoints/pollgearman.bml
--- a/htdocs/tools/endpoints/pollgearman.bml	Thu Feb 24 21:56:24 2011 +0800
+++ b/htdocs/tools/endpoints/pollgearman.bml	Fri Feb 25 20:41:07 2011 +0800
@@ -55,6 +55,6 @@ _c?>
 
     sleep(1.5) if $LJ::IS_DEV_SERVER;
 
-    return LJ::js_dumper($ret);
+    return JSON::objToJson( $ret );
 }
 _code?>
diff -r f4d51a6cd107 -r ea69e4ab1d4e htdocs/tools/endpoints/pollvote.bml
--- a/htdocs/tools/endpoints/pollvote.bml	Thu Feb 24 21:56:24 2011 +0800
+++ b/htdocs/tools/endpoints/pollvote.bml	Fri Feb 25 20:41:07 2011 +0800
@@ -82,7 +82,7 @@ _c?>
     
     sleep(1.5) if $LJ::IS_DEV_SERVER;
 
-    return LJ::js_dumper($ret);
+    return JSON::objToJson( $ret );
 }
 
 _code?>
diff -r f4d51a6cd107 -r ea69e4ab1d4e htdocs/tools/endpoints/widget.bml
--- a/htdocs/tools/endpoints/widget.bml	Thu Feb 24 21:56:24 2011 +0800
+++ b/htdocs/tools/endpoints/widget.bml	Fri Feb 25 20:41:07 2011 +0800
@@ -24,7 +24,7 @@ _c?>
     my $err = sub {
         my $msg = shift;
         my %extra = @_;
-        return LJ::js_dumper({
+        return JSON::objToJson({
             error => "$msg",
         });
     };
@@ -111,6 +111,6 @@ _c?>
 
 #    sleep(2) if $LJ::IS_DEV_SERVER;
 
-    return LJ::js_dumper(\%ret);
+    return JSON::objToJson( \%ret );
 }
 _code?>
--------------------------------------------------------------------------------