changeset 482:ae0cbdc9008a

Fixes PR588, Icedtea-web now saves cookies set in java cookie jar
author Adam Domurad <adomurad@redhat.com>
date Fri, 17 Aug 2012 11:46:58 -0400
parents 5586c774341b
children 3564e46e0a4f
files ChangeLog NEWS plugin/icedteanp/IcedTeaNPPlugin.cc plugin/icedteanp/java/sun/applet/PluginCookieManager.java plugin/icedteanp/java/sun/applet/PluginMain.java
diffstat 5 files changed, 91 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Fri Aug 17 11:40:49 2012 -0400
+++ b/ChangeLog	Fri Aug 17 11:46:58 2012 -0400
@@ -1,3 +1,16 @@
+2012-08-17  Adam Domurad  <adomurad@redhat.com>
+
+	Fixes PR588, cookies set in the java cookie jar are now stored properly
+	* plugin/icedteanp/IcedTeaNPPlugin.cc
+	(set_cookie_info): New, uses setvalueforurl
+	(consume_plugin_message): Additional message added allowing 
+	set_cookie_info to be used from the java side.
+	* plugin/icedteanp/java/sun/applet/PluginCookieManager.java: Now
+	overrides put method, results in set_cookie_info calls in C++
+	* plugin/icedteanp/java/sun/applet/PluginMain.java: Passes
+	PluginStreamHandler to PluginCookieManager to allow C++ side
+	communication
+
 2012-08-17  Adam Domurad  <adomurad@redhat.com>
 
 	* plugin/icedteanp/IcedTeaNPPlugin.cc
--- a/NEWS	Fri Aug 17 11:40:49 2012 -0400
+++ b/NEWS	Fri Aug 17 11:46:58 2012 -0400
@@ -26,6 +26,7 @@
   - PR1011: Folders treated as jar files in archive tag
   - PR1106: Buffer overflow in plugin table
   - PR975: Plugin should not include classpaths specified in jar manifests when using jnlp_href
+  - PR588: Cookies not written from cookie jar to browser cookies
 * Common
   - PR918: java applet windows uses a low resulution black/white icon
   - RH838417: Disambiguate signed applet security prompt from certificate warning
--- a/plugin/icedteanp/IcedTeaNPPlugin.cc	Fri Aug 17 11:40:49 2012 -0400
+++ b/plugin/icedteanp/IcedTeaNPPlugin.cc	Fri Aug 17 11:46:58 2012 -0400
@@ -985,6 +985,21 @@
   return NPERR_NO_ERROR;
 }
 
+static NPError
+set_cookie_info(const char* siteAddr, const char* cookieString, uint32_t len)
+{
+  // Only attempt to perform this operation if there is a valid plugin instance
+  if (g_hash_table_size(instance_to_id_map) > 0 && browser_functions.getvalueforurl)
+  {
+      // We arbitrarily use the first valid instance we can grab
+      // For an explanation of the logic behind this, see get_cookie_info
+      gpointer instance = getFirstInTableInstance(instance_to_id_map);
+      return browser_functions.setvalueforurl((NPP) instance, NPNURLVCookie, siteAddr, cookieString, len);
+  }
+
+  return NPERR_GENERIC_ERROR;;
+}
+
 // HELPER FUNCTIONS
 
 static void
@@ -1247,7 +1262,37 @@
     decoded_url = NULL;
     g_free(cookie_info);
     cookie_info = NULL;
+  } else if (g_str_has_prefix(parts[1], "PluginSetCookie"))
+  {
+    // Message structure: plugin PluginSetCookie reference -1 <url> <cookie>
+    gchar** cookie_parts = g_strsplit (message, " ", 6);
+
+    if (g_strv_length(cookie_parts) < 6)
+    {
+       g_strfreev (parts);
+       g_strfreev (cookie_parts);
+       return; // Defensive, message _should_ be properly formatted
+    }
+
+    gchar* decoded_url = (gchar*) calloc(strlen(cookie_parts[4])+1, sizeof(gchar));
+    IcedTeaPluginUtilities::decodeURL(cookie_parts[4], &decoded_url);
+
+    gchar* cookie_string = cookie_parts[5];
+    uint32_t len = strlen(cookie_string);
+    if (set_cookie_info(decoded_url, cookie_string, len) == NPERR_NO_ERROR)
+    {
+  	  PLUGIN_DEBUG("Setting cookie for URL %s to %s\n", decoded_url, cookie_string);
+    } else
+    {
+  	  PLUGIN_DEBUG("Not able to set cookie for URL %s to %s\n", decoded_url, cookie_string);
+    }
+
+    free(decoded_url);
+    decoded_url = NULL;
+    g_strfreev (cookie_parts);
+    cookie_parts = NULL;
   }
+
   g_strfreev (parts);
   parts = NULL;
 }
--- a/plugin/icedteanp/java/sun/applet/PluginCookieManager.java	Fri Aug 17 11:40:49 2012 -0400
+++ b/plugin/icedteanp/java/sun/applet/PluginCookieManager.java	Fri Aug 17 11:46:58 2012 -0400
@@ -45,7 +45,16 @@
 import java.util.List;
 import java.util.Map;
 
+import com.sun.jndi.toolkit.url.UrlUtil;
+
 public class PluginCookieManager extends CookieManager {
+    private PluginStreamHandler streamHandler;
+
+    public PluginCookieManager(PluginStreamHandler streamHandler) {
+        this.streamHandler = streamHandler;
+    }
+
+    @Override
     public Map<String, List<String>> get(URI uri,
             Map<String, List<String>> requestHeaders) throws IOException {
         // pre-condition check
@@ -84,4 +93,21 @@
 
         return false;
     }
+
+    @Override
+    public void put(URI uri,
+            Map<String, List<String>> responseHeaders) throws IOException {
+        super.put(uri, responseHeaders);
+
+        for (Map.Entry<String, List<String>> headerEntry : responseHeaders.entrySet()) {
+            String type = headerEntry.getKey();
+            if ("Set-Cookie".equalsIgnoreCase(type) || "Set-Cookie2".equalsIgnoreCase(type)) {
+                List<String> cookies = headerEntry.getValue();
+                for (String cookie : cookies) {
+                    streamHandler.write("plugin PluginSetCookie reference -1 " + UrlUtil.encode(uri.toString(), "UTF-8") + " " + cookie);
+                }
+            }
+
+        }
+    }
 }
--- a/plugin/icedteanp/java/sun/applet/PluginMain.java	Fri Aug 17 11:40:49 2012 -0400
+++ b/plugin/icedteanp/java/sun/applet/PluginMain.java	Fri Aug 17 11:46:58 2012 -0400
@@ -118,6 +118,9 @@
 
             // Streams set. Start processing.
             streamHandler.startProcessing();
+
+            setCookieHandler(streamHandler);
+
         } catch (Exception e) {
             e.printStackTrace();
             System.err.println("Something very bad happened. I don't know what to do, so I am going to exit :(");
@@ -199,8 +202,10 @@
         }
         // override the proxy selector set by JNLPRuntime
         ProxySelector.setDefault(new PluginProxySelector());
+    }
 
-        CookieManager ckManager = new PluginCookieManager();
+    private static void setCookieHandler(PluginStreamHandler streamHandler) {
+        CookieManager ckManager = new PluginCookieManager(streamHandler);
         CookieHandler.setDefault(ckManager);
     }
 }