kristianhristov / cordova-cookie-master

MIT License
26 stars 113 forks source link

Android, Remove the extra quotes of cookies, while getCookie #19

Closed ReachingTechnology closed 4 years ago

ReachingTechnology commented 6 years ago

The RFC of cookie says cookie value can be set with or without quotes, so some webserver will return cookies with quotes at the beginning and the end. When cookieEmperor trying to geneate a json for cookie value, it will throw exception for the unescaped quotes. My solution is removing these two quotes. Please see the code.

one statement added: cookieValue = cookieValue.replaceAll("^\"|\"$", "");

Thanks!

private boolean getCookie(JSONArray args, final CallbackContext callbackContext) { try { final String url = args.getString(0); final String cookieName = args.getString(1);

        cordova
                .getThreadPool()
                .execute(new Runnable() {
                    public void run() {
                        try {
                            CookieManager cookieManager = CookieManager.getInstance();
                            String[] cookies = cookieManager.getCookie(url).split("; ");
                            String cookieValue = "";

                            for (int i = 0; i < cookies.length; i++) {
                                if (cookies[i].contains(cookieName + "=")) {
                                    cookieValue = cookies[i].split("=")[1].trim();
                                    break;
                                }
                            }

                            JSONObject json = null;

                            if (cookieValue != "") {
                                cookieValue = cookieValue.replaceAll("^\"|\"$", "");
                                json = new JSONObject("{cookieValue:\"" + cookieValue + "\"}");
                            }

                            if (json != null) {
                                PluginResult res = new PluginResult(PluginResult.Status.OK, json);
                                callbackContext.sendPluginResult(res);
                            }
                            else {
                                callbackContext.error("Cookie not found!");
                            }
                        }
                        catch (Exception e) {
                            callbackContext.error(e.getMessage());
                        }
                    }
                });

        return true;
    }
    catch(JSONException e) {
        callbackContext.error("JSON parsing error");
    }

    return false;
}
kristianhristov commented 4 years ago

I see you have created your own repository and a new plugin based on mine. Please be careful not to submit changes that correspond to your repository into this one.