Author: chabotc
Date: Tue Feb 24 00:08:49 2009
New Revision: 747229

URL: http://svn.apache.org/viewvc?rev=747229&view=rev
Log:
This adds support for rewriting proxied CSS files if the gadget query param has 
been provided, and the css file falls within it's content-rewrite rules. This 
finishes the basic content-rewrite feature support, yay

Modified:
    incubator/shindig/trunk/php/src/common/Config.php
    incubator/shindig/trunk/php/src/gadgets/ProxyHandler.php
    incubator/shindig/trunk/php/src/gadgets/rewrite/ContentRewriter.php

Modified: incubator/shindig/trunk/php/src/common/Config.php
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/php/src/common/Config.php?rev=747229&r1=747228&r2=747229&view=diff
==============================================================================
--- incubator/shindig/trunk/php/src/common/Config.php (original)
+++ incubator/shindig/trunk/php/src/common/Config.php Tue Feb 24 00:08:49 2009
@@ -35,7 +35,7 @@
       include_once 'config/container.php';
       self::$config = $shindigConfig;
       if (file_exists('config/local.php')) {
-        // include local.php if it exists and merge the config arrays. 
+        // include local.php if it exists and merge the config arrays.
         // the second array values overwrites the first one's
         include_once 'config/local.php';
         self::$config = array_merge(self::$config, $shindigConfig);

Modified: incubator/shindig/trunk/php/src/gadgets/ProxyHandler.php
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/php/src/gadgets/ProxyHandler.php?rev=747229&r1=747228&r2=747229&view=diff
==============================================================================
--- incubator/shindig/trunk/php/src/gadgets/ProxyHandler.php (original)
+++ incubator/shindig/trunk/php/src/gadgets/ProxyHandler.php Tue Feb 24 
00:08:49 2009
@@ -18,9 +18,6 @@
  * under the License.
  */
 
-// according to features/core/io.js, this is high on the list of things to 
scrap
-define('UNPARSEABLE_CRUFT', "throw 1; < don't be evil' >");
-
 /**
  * The ProxyHandler class does the actual proxy'ing work. it deals both with
  * GET and POST based input, and peforms a request based on the input, headers 
and
@@ -52,7 +49,7 @@
       }
     }
     if (! $isShockwaveFlash) {
-      header('Content-Disposition: attachment;filename=p.txt');
+      //header('Content-Disposition: attachment;filename=p.txt');
     }
     $lastModified = $result->getResponseHeader('Last-Modified') != null ? 
$result->getResponseHeader('Last-Modified') : gmdate('D, d M Y H:i:s', 
$result->getCreated()) . ' GMT';
     $notModified = false;
@@ -67,6 +64,10 @@
     if ($httpCode == 200) {
       // only set caching headers if the result was 'OK'
       $this->setCachingHeaders($lastModified);
+      // was the &gadget=<gadget url> specified in the request? if so parse it 
and check the rewrite settings
+      if (isset($_GET['gadget'])) {
+        $this->rewriteContent($_GET['gadget'], $result);
+      }
     }
     // If the cached file time is within the refreshInterval params value, 
return not-modified
     if ($notModified) {
@@ -77,4 +78,55 @@
       echo $result->getResponseContent();
     }
   }
+
+  private function rewriteContent($gadgetUrl, RemoteContentRequest &$result) {
+    try {
+      // At the moment we're only able to rewrite CSS files, so check the 
content type and/or the file extension before rewriting
+      $headers = $result->getResponseHeaders();
+      $isCss = false;
+      if (isset($headers['Content-Type']) && 
strtolower($headers['Content-Type'] == 'text/csss')) {
+        $isCss = true;
+      } else {
+        $ext = substr($_GET['url'], strrpos($_GET['url'], '.') + 1);
+        $isCss = strtolower($ext) == 'css';
+      }
+      if ($isCss) {
+        $gadget = $this->createGadget($gadgetUrl);
+        $rewrite = $gadget->gadgetSpec->rewrite;
+        if (is_array($rewrite)) {
+          $contentRewriter = new ContentRewriter($this->context, $gadget);
+          
$result->setResponseContent($contentRewriter->rewriteCSS($result->getResponseContent()));
+        }
+      }
+    } catch (Exception $e) {
+      // ignore, not being able to rewrite anything isn't fatal
+    }
+
+  }
+
+  /**
+   * Uses the GadgetFactory to instrance the specified gadget
+   *
+   * @param string $gadgetUrl
+   */
+  private function createGadget($gadgetUrl) {
+    // Only include these files if appropiate, else it would slow down the 
entire proxy way to much
+    require_once 'src/gadgets/GadgetSpecParser.php';
+    require_once 'src/gadgets/GadgetBlacklist.php';
+    require_once 'src/gadgets/sample/BasicGadgetBlacklist.php';
+    require_once 'src/gadgets/GadgetContext.php';
+    require_once 'src/gadgets/GadgetFactory.php';
+    require_once 'src/gadgets/GadgetSpec.php';
+    require_once 'src/gadgets/Gadget.php';
+    require_once 'src/gadgets/GadgetException.php';
+    require_once 'src/gadgets/rewrite/GadgetRewriter.php';
+    require_once 'src/gadgets/rewrite/DomRewriter.php';
+    require_once 'src/gadgets/rewrite/ContentRewriter.php';
+    // make sure our context returns the gadget url and not the proxied 
document url
+    $this->context->setUrl($gadgetUrl);
+    // and create & return the gadget
+    $gadgetSpecFactory = new GadgetFactory($this->context, null);
+    $gadget = $gadgetSpecFactory->createGadget();
+    return $gadget;
+  }
 }

Modified: incubator/shindig/trunk/php/src/gadgets/rewrite/ContentRewriter.php
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/php/src/gadgets/rewrite/ContentRewriter.php?rev=747229&r1=747228&r2=747229&view=diff
==============================================================================
--- incubator/shindig/trunk/php/src/gadgets/rewrite/ContentRewriter.php 
(original)
+++ incubator/shindig/trunk/php/src/gadgets/rewrite/ContentRewriter.php Tue Feb 
24 00:08:49 2009
@@ -109,13 +109,23 @@
   }
 
   /**
-   * Tries to find url(<url tag>) constructs and rewrite them to their
+   * Uses rewriteCSS to find url(<url tag>) constructs and rewrite them to 
their
    * proxied counterparts
    *
    * @param DOMElement $node
    */
   public function rewriteStyle(DOMElement &$node) {
-    $content = $node->nodeValue;
+    $node->nodeValue = $this->rewriteCSS($node->nodeValue);
+  }
+
+  /**
+   * Does the actual CSS rewriting, this is a seperate function so it can be 
called
+   * from the proxy handler too
+   *
+   * @param string $content
+   * @return string
+   */
+  public function rewriteCSS($content) {
     $newVal = '';
     // loop through the url elements in the content
     while (($pos = strpos($content, 'url')) !== false) {
@@ -138,9 +148,14 @@
     }
     // append what's left
     $newVal .= $content;
-    $node->nodeValue = $newVal;
+    return $newVal;
   }
 
+  /**
+   * Rewrites <script src="http://example.org/foo.js"; /> tags into their 
proxied versions
+   *
+   * @param DOMElement $node
+   */
   public function rewriteScript(DOMElement &$node) {
     if (($src = $node->getAttribute('src')) != null && 
$this->includedUrl($src)) {
       // make sure not to rewrite our forcedJsLibs src tag, else things break
@@ -150,6 +165,11 @@
     }
   }
 
+  /**
+   * Rewrites <link href="http://example.org/foo.css"; /> tags into their 
proxied versions
+   *
+   * @param DOMElement $node
+   */
   public function rewriteStyleLink(DOMElement &$node) {
     if (($src = $node->getAttribute('href')) != null && 
$this->includedUrl($src)) {
       $node->setAttribute('href', $this->getProxyUrl($src));


Reply via email to