changeset 499:bb3f67ab9fb8

Fixes PR588, Icedtea-web now saves cookies set in java cookie jar
author Adam Domurad <adomurad@redhat.com>
date Fri, 17 Aug 2012 10:40:22 -0400
parents cbc297c8c6ee
children 5842f6c899d0
files NEWS plugin/icedteanp/IcedTeaNPPlugin.cc plugin/icedteanp/java/sun/applet/PluginCookieManager.java plugin/icedteanp/java/sun/applet/PluginMain.java
diffstat 4 files changed, 78 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/NEWS	Fri Aug 17 10:26:26 2012 -0400
+++ b/NEWS	Fri Aug 17 10:40:22 2012 -0400
@@ -31,6 +31,7 @@
   - PR722: META-INF/ unsigned entries should be ignored in signing
   - PR855: AppletStub getDocumentBase() doesn't return full URL
   - PR1011: Folders treated as jar files in archive tag
+  - 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 10:26:26 2012 -0400
+++ b/plugin/icedteanp/IcedTeaNPPlugin.cc	Fri Aug 17 10:40:22 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 10:26:26 2012 -0400
+++ b/plugin/icedteanp/java/sun/applet/PluginCookieManager.java	Fri Aug 17 10:40:22 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 10:26:26 2012 -0400
+++ b/plugin/icedteanp/java/sun/applet/PluginMain.java	Fri Aug 17 10:40:22 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);
     }
 }