You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@shindig.apache.org by ch...@apache.org on 2009/09/20 14:26:23 UTC

svn commit: r817015 - in /incubator/shindig/trunk/php/src: common/XmlError.php common/sample/BasicRemoteContentFetcher.php gadgets/MakeRequest.php gadgets/MakeRequestHandler.php gadgets/ProxyBase.php gadgets/ProxyHandler.php

Author: chabotc
Date: Sun Sep 20 12:26:22 2009
New Revision: 817015

URL: http://svn.apache.org/viewvc?rev=817015&view=rev
Log:
Fixes utf8 escaping in the makeRequest's json_encoding and correctly parse Link elements in feed entries

Modified:
    incubator/shindig/trunk/php/src/common/XmlError.php
    incubator/shindig/trunk/php/src/common/sample/BasicRemoteContentFetcher.php
    incubator/shindig/trunk/php/src/gadgets/MakeRequest.php
    incubator/shindig/trunk/php/src/gadgets/MakeRequestHandler.php
    incubator/shindig/trunk/php/src/gadgets/ProxyBase.php
    incubator/shindig/trunk/php/src/gadgets/ProxyHandler.php

Modified: incubator/shindig/trunk/php/src/common/XmlError.php
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/php/src/common/XmlError.php?rev=817015&r1=817014&r2=817015&view=diff
==============================================================================
--- incubator/shindig/trunk/php/src/common/XmlError.php (original)
+++ incubator/shindig/trunk/php/src/common/XmlError.php Sun Sep 20 12:26:22 2009
@@ -57,4 +57,25 @@
     $ret .= trim($error->message) . "\n  Line: $error->line" . "\n  Column: $error->column\n\n";
     return $ret;
   }
+
+  /**
+   * Generic misc debugging function to dump a node's structure as plain text xml
+   *
+   * @param DOMElement $node
+   * @param string $function
+   */
+  public function dumpNode($node, $function) {
+    $doc = new DOMDocument(null, 'utf-8');
+    $doc->preserveWhiteSpace = true;
+    $doc->formatOutput = false;
+    $doc->strictErrorChecking = false;
+    $doc->recover = false;
+    $doc->resolveExternals = false;
+    if (! $newNode = @$doc->importNode($node, false)) {
+      echo "[Invalid node, dump failed]<br><br>";
+      return;
+    }
+    $doc->appendChild($newNode);
+    echo "<b>$function (" . get_class($node) . "):</b><br>" . htmlentities(str_replace('<?xml version="" encoding="utf-8"?>', '', $doc->saveXML()) . "\n") . "<br><br>";
+  }
 }

Modified: incubator/shindig/trunk/php/src/common/sample/BasicRemoteContentFetcher.php
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/php/src/common/sample/BasicRemoteContentFetcher.php?rev=817015&r1=817014&r2=817015&view=diff
==============================================================================
--- incubator/shindig/trunk/php/src/common/sample/BasicRemoteContentFetcher.php (original)
+++ incubator/shindig/trunk/php/src/common/sample/BasicRemoteContentFetcher.php Sun Sep 20 12:26:22 2009
@@ -207,6 +207,7 @@
     $handle = curl_init();
     curl_setopt($handle, CURLOPT_URL, $url);
     curl_setopt($handle, CURLOPT_FOLLOWLOCATION, 1);
+    curl_setopt($handle, CURLOPT_BINARYTRANSFER, 1);
     curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);
     curl_setopt($handle, CURLOPT_AUTOREFERER, 1);
     curl_setopt($handle, CURLOPT_MAXREDIRS, 10);

Modified: incubator/shindig/trunk/php/src/gadgets/MakeRequest.php
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/php/src/gadgets/MakeRequest.php?rev=817015&r1=817014&r2=817015&view=diff
==============================================================================
--- incubator/shindig/trunk/php/src/gadgets/MakeRequest.php (original)
+++ incubator/shindig/trunk/php/src/gadgets/MakeRequest.php Sun Sep 20 12:26:22 2009
@@ -49,7 +49,7 @@
    *     to override the default fetcher which will be loaded from the config
    *     file.  This allows for injecting a mock into this class for testing.
    */
-  public function __construct($remoteFetcher = null){
+  public function __construct($remoteFetcher = null) {
     if (isset($remoteFetcher)) {
       $this->remoteFetcher = $remoteFetcher;
     } else {
@@ -82,15 +82,12 @@
       $gadgetSigner = new $gadgetSigner();
       $signingFetcherFactory = new SigningFetcherFactory(Config::get("private_key_file"));
     }
-
     $basicRemoteContent = new BasicRemoteContent($this->remoteFetcher, $signingFetcherFactory, $gadgetSigner);
-
     $request = $this->buildRequest($context, $params, $gadgetSigner);
     $request->getOptions()->ignoreCache = $params->getNoCache();
     $request->getOptions()->viewerSigned = $params->getSignViewer();
-    $request->getOptions()->ownerSigned = $params->getSignOwner(); 
+    $request->getOptions()->ownerSigned = $params->getSignOwner();
     $result = $basicRemoteContent->fetch($request);
-
     $status = (int)$result->getHttpCode();
     if ($status == 200) {
       switch ($params->getResponseFormat()) {
@@ -100,7 +97,9 @@
           break;
       }
     }
-
+    if (strpos($result->getResponseContent(), '\u')) {
+    	$result->setResponseContent($this->decodeUtf8($result->getResponseContent()));
+    }
     return $result;
   }
 
@@ -118,23 +117,20 @@
     $protocolSplit = explode('://', $params->getHref(), 2);
     if (count($protocolSplit) < 2) {
       throw new Exception("Invalid protocol specified");
-    } 
-
+    }
     $protocol = strtoupper($protocolSplit[0]);
     if ($protocol != "HTTP" && $protocol != "HTTPS") {
       throw new Exception("Invalid protocol specified in url: " . htmlentities($protocol));
     }
-
     $method = $params->getHttpMethod();
     if ($method == 'POST' || $method == 'PUT') {
-       // even if postData is an empty string, it will still post
-       // (since RemoteContentRquest checks if its false)
-       // so the request to POST is still honored
-       $request = new RemoteContentRequest($params->getHref(), null, $params->getRequestBody());
+      // even if postData is an empty string, it will still post
+      // (since RemoteContentRquest checks if its false)
+      // so the request to POST is still honored
+      $request = new RemoteContentRequest($params->getHref(), null, $params->getRequestBody());
     } else {
-       $request = new RemoteContentRequest($params->getHref());
+      $request = new RemoteContentRequest($params->getHref());
     }
-
     if ($signer) {
       switch ($params->getAuthz()) {
         case 'SIGNED':
@@ -145,7 +141,6 @@
           $request->setOAuthRequestParams($params->getOAuthRequestParameters());
           break;
       }
-      
       $st = $params->getSecurityTokenString();
       if ($st === false) {
         throw new Exception("A security token is required for signed requests");
@@ -153,7 +148,6 @@
       $token = $context->validateToken($st, $signer);
       $request->setToken($token);
     }
-
     $headers = $params->getFormattedRequestHeaders();
     if ($headers !== false) {
       // The request expects headers to be stored as a normal header text blob.
@@ -161,10 +155,18 @@
       //     Accept-Language: en-us
       $request->setHeaders($headers);
     }
-    
     return $request;
   }
 
+  public function decodeUtf8($content) {
+    if (preg_match("/&#[xX][0-9a-zA-Z]{2,8};/", $content)) {
+      $content = preg_replace("/&#[xX]([0-9a-zA-Z]{2,8});/e", "'&#'.hexdec('$1').';'", $content);
+    }
+    if (preg_match("/\\\\[uU][0-9a-zA-Z]{2,8}/", $content)) {
+      $content = preg_replace("/\\\\[uU]([0-9a-zA-Z]{2,8})/e", "'&#'.hexdec('$1').';'", $content);
+    }
+    return mb_decode_numericentity($content, array(0x0, 0xFFFF, 0, 0xFFFF), 'UTF-8');
+  }
 
   /**
    * Handles (RSS & Atom) Type.FEED parsing using Zend's feed parser
@@ -199,6 +201,9 @@
             $_entry = array();
             $_entry['Title'] = $item->title();
             $_entry['Link'] = $item->link();
+            if (!is_string($_entry['Link']) && isset($_entry['Link'][1]) && $_entry['Link'][1] instanceof DOMElement) {
+            	$_entry['Link'] = $_entry['Link'][1]->getAttribute('href');
+            }
             if ($getSummaries && $item->description()) {
               $_entry['Summary'] = $item->description();
             }

Modified: incubator/shindig/trunk/php/src/gadgets/MakeRequestHandler.php
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/php/src/gadgets/MakeRequestHandler.php?rev=817015&r1=817014&r2=817015&view=diff
==============================================================================
--- incubator/shindig/trunk/php/src/gadgets/MakeRequestHandler.php (original)
+++ incubator/shindig/trunk/php/src/gadgets/MakeRequestHandler.php Sun Sep 20 12:26:22 2009
@@ -37,25 +37,26 @@
     $this->context = $context;
     $this->makeRequest = new MakeRequest();
   }
-  
+
   /**
    * Fetches content and echoes it in JSON format
-   * 
+   *
    * @param MakeRequestOptions $params  The request configuration.
    */
   public function fetchJson(MakeRequestOptions $params) {
     $result = $this->makeRequest->fetch($this->context, $params);
-
     $responseArray = array(
-      'body' => $result->getResponseContent(),
       'rc' => (int)$result->getHttpCode(),
-      'headers' => $result->getResponseHeaders()
+      'body' => $result->getResponseContent()
+      //FIXME: the spec seems to state the response should contain the headers, however java shindig doesn't (and it's a waste of bandwidth 99% of the time), check to see what is correct
+      //'headers' => $result->getResponseHeaders()
     );
-    
     $responseArray = array_merge($responseArray, $result->getMetadatas());
-
     $json = array($params->getHref() => $responseArray);
     $json = json_encode($json);
+    if (strpos($json, '\u')) {
+      $json = $this->makeRequest->decodeUtf8($json);
+    }
     $output = UNPARSEABLE_CRUFT . $json;
     if ($responseArray['rc'] == 200) {
       // only set caching headers if the result was 'OK'

Modified: incubator/shindig/trunk/php/src/gadgets/ProxyBase.php
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/php/src/gadgets/ProxyBase.php?rev=817015&r1=817014&r2=817015&view=diff
==============================================================================
--- incubator/shindig/trunk/php/src/gadgets/ProxyBase.php (original)
+++ incubator/shindig/trunk/php/src/gadgets/ProxyBase.php Sun Sep 20 12:26:22 2009
@@ -29,10 +29,6 @@
    */
   public $context;
 
-  protected $disallowedHeaders = array('User-Agent', 'Keep-Alive', 'Host', 'Accept-Encoding', 'Set-Cookie',
-      'Content-Length', 'Content-Encoding', 'ETag', 'Last-Modified', 'Accept-Ranges', 'Vary',
-      'Expires', 'Date', 'Pragma', 'Cache-Control', 'Transfer-Encoding', 'If-Modified-Since');
-
   public function __construct($context) {
     $this->context = $context;
     $this->makeRequest = new MakeRequest();

Modified: incubator/shindig/trunk/php/src/gadgets/ProxyHandler.php
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/php/src/gadgets/ProxyHandler.php?rev=817015&r1=817014&r2=817015&view=diff
==============================================================================
--- incubator/shindig/trunk/php/src/gadgets/ProxyHandler.php (original)
+++ incubator/shindig/trunk/php/src/gadgets/ProxyHandler.php Sun Sep 20 12:26:22 2009
@@ -25,6 +25,9 @@
  *
  */
 class ProxyHandler extends ProxyBase {
+  protected $disallowedHeaders = array('User-Agent', 'Keep-Alive', 'Host', 'Accept-Encoding', 'Set-Cookie',
+      'Content-Length', 'Content-Encoding', 'ETag', 'Last-Modified', 'Accept-Ranges', 'Vary',
+      'Expires', 'Date', 'Pragma', 'Cache-Control', 'Transfer-Encoding', 'If-Modified-Since');
 
   /**
    * Fetches the content and returns it as-is using the headers as returned