madeon (madeon) wrote in changelog,
madeon
madeon
changelog

[livejournal] r22595: LJSUP-13199: Ability to use JSON RPC fro...

Committer: sbelyaev
LJSUP-13199: Ability to use JSON RPC from l-stat
U   trunk/cgi-bin/LJ/JSON/RPC.pm
Modified: trunk/cgi-bin/LJ/JSON/RPC.pm
===================================================================
--- trunk/cgi-bin/LJ/JSON/RPC.pm	2012-08-08 07:17:42 UTC (rev 22594)
+++ trunk/cgi-bin/LJ/JSON/RPC.pm	2012-08-08 10:52:16 UTC (rev 22595)
@@ -80,9 +80,8 @@
     my $call_info = {
         'source'        => 'jsonrpc',
         'type'          => $self->{'callback'} ? 'jsonp' : 'CORS',
-        'hostname'      => LJ::Request->hostname,
+        'access_data'   => $item->access_data,
         'access_type'   => $item->access_type,
-        'access_data'   => $item->access_data,
     };
 
     $entry->{'result'} = $callback->($method, $params, $call_info);
@@ -92,6 +91,7 @@
     my ($self) = @_;
     my $items = $self->{'items'};
     my $resp_data;
+    my $headers = {};
     
     if ($self->{'is_array'}) {
         foreach my $entry (@$items) {
@@ -102,13 +102,41 @@
         }
     } else {
         my $item = $items->{'item'};
-        $resp_data = $item->response($items->{'result'});
+        my $result = $items->{'result'};
+
+        $resp_data = $item->response($result);
+        if ($self->{'callback'} && !$resp_data->{'error'}) {
+            $headers = __get_headers($result);
+        }
     }
 
     return LJ::Response::JSON->new( 
-                'data'     => $resp_data,
-                'callback' => $self->{'callback'} );
+                'data'          => $resp_data,
+                'callback'      => $self->{'callback'},
+                'http_headers'  => $headers, );
 
 }
 
+sub __get_headers {
+    my ($self, $result) = @_;
+    if (LJ::Request->hostname eq 'stat.' . $LJ::DOMAIN) {
+        if ($result->{'properties'}) {
+            my $properties = $result->{'properties'};
+            if ($properties->{'cache-time'}) {
+                my $max_age = $properties->{'cache-time'};
+                my $expires_out = HTTP::Date::time2str( time + $max_age );
+
+                return  { 'Cache-Control' => "public, max-age=$max_age",
+                          'Expires'       => $expires_out, };
+            }
+        }
+
+        return { 'Cache-Control' => 'private, must-revalidate',
+                 'Expires'       => '0', };
+
+    }
+
+    return;
+}
+
 1;

Tags: livejournal, madeon, pm, sbelyaev
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments