/** Tests explicit root path with a non-trivial URL fetch from the domain */ public void testRootPath1() throws Exception { URL url = new URL("http://d.e.f/moo.html"); man.addCookieFromHeader("test=moo;path=/", url); String s = man.getCookieHeaderForURL(new URL("http://d.e.f/goo.html")); assertNotNull(s); assertEquals("test=moo", s); }
// Test session cookie is returned public void testSessionCookie() throws Exception { URL url = new URL("http://a.b.c/"); man.addCookieFromHeader("test=1", url); String s = man.getCookieHeaderForURL(url); assertNotNull(s); assertEquals("test=1", s); }
public void testCookies2() throws Exception { URL url = new URL("https://a.b.c.d/testCookies2"); man.addCookieFromHeader("test1=1;secure, test2=2;secure", url); assertEquals(2, man.getCookieCount()); String s = man.getCookieHeaderForURL(url); assertNotNull(s); assertEquals("test1=1; test2=2", s); }
// Test multi-cookie header handling public void testCookies1() throws Exception { URL url = new URL("http://a.b.c.d/testCookies1"); man.addCookieFromHeader("test1=1; comment=\"how,now\", test2=2; version=1", url); assertEquals(2, man.getCookieCount()); String s = man.getCookieHeaderForURL(url); assertNotNull(s); assertEquals("test1=1; test2=2", s); }
// Test New cookie is returned public void testNewCookie() throws Exception { URL url = new URL("http://a.b.c/"); man.addCookieFromHeader("test=1; expires=Mon, 01-Jan-2990 00:00:00 GMT", url); assertEquals(1, man.getCookieCount()); String s = man.getCookieHeaderForURL(url); assertNotNull(s); assertEquals("test=1", s); }
@Override public void setUp() throws Exception { super.setUp(); jmctx = JMeterContextService.getContext(); man = new CookieManager(); man.setThreadContext(jmctx); man.testStarted(); // This is needed in order to set up the cookie policy }
public void testSendCookie2() throws Exception { man.add(new Cookie("id", "value", ".apache.org", "/", false, 9999999999L)); HTTPSamplerBase sampler = new HTTPNullSampler(); sampler.setDomain("jakarta.apache.org"); sampler.setPath("/index.html"); sampler.setMethod(HTTPSamplerBase.GET); assertNotNull(man.getCookieHeaderForURL(sampler.getUrl())); }
public void testCrossDomainHandling() throws Exception { URL url = new URL("http://jakarta.apache.org/"); assertEquals(0, man.getCookieCount()); // starts empty man.addCookieFromHeader("test=2;domain=.hc.apache.org", url); assertEquals(0, man.getCookieCount()); // should not be stored man.addCookieFromHeader("test=1;domain=.jakarta.apache.org", url); assertEquals(1, man.getCookieCount()); // OK }
// Bug 2063 public void testCookieWithEquals() throws Exception { URL url = new URL("http://a.b.c/"); man.addCookieFromHeader("NSCP_USER_LOGIN1_NEW=SHA=xxxxx", url); String s = man.getCookieHeaderForURL(url); assertNotNull(s); assertEquals("NSCP_USER_LOGIN1_NEW=SHA=xxxxx", s); Cookie c = man.get(0); assertEquals("NSCP_USER_LOGIN1_NEW", c.getName()); assertEquals("SHA=xxxxx", c.getValue()); }
public void testCookieOrdering1() throws Exception { URL url = new URL("http://order.now/sub1/moo.html"); man.addCookieFromHeader("test1=moo1;path=/", url); man.addCookieFromHeader("test2=moo2;path=/sub1", url); man.addCookieFromHeader("test2=moo3;path=/", url); assertEquals(3, man.getCookieCount()); String s = man.getCookieHeaderForURL(url); assertNotNull(s); assertEquals("test2=moo2; test1=moo1; test2=moo3", s); }
/** * Add the content related headers. These headers contain user private data and is not used when * we are proxying an untrusted request. */ private void populateHeaders() { if (mReferrer != null) mHeaders.put("Referer", mReferrer); if (mContentType != null) mHeaders.put(CONTENT_TYPE, mContentType); // if we have an active proxy and have proxy credentials, do pre-emptive // authentication to avoid an extra round-trip: if (mNetwork.isValidProxySet()) { String username; String password; /* The proxy credentials can be set in the Network thread */ synchronized (mNetwork) { username = mNetwork.getProxyUsername(); password = mNetwork.getProxyPassword(); } if (username != null && password != null) { // we collect credentials ONLY if the proxy scheme is BASIC!!! String proxyHeader = RequestHandle.authorizationHeader(true); mHeaders.put( proxyHeader, "Basic " + RequestHandle.computeBasicAuthResponse(username, password)); } } // Set cookie header String cookie = CookieManager.getInstance().getCookie(mListener.getWebAddress()); if (cookie != null && cookie.length() > 0) { mHeaders.put("Cookie", cookie); } }
/** * Store a cookie string associated with a url. * * @param url The url to be used as a key for the cookie. * @param value The cookie string to be stored. */ @DSComment("Private Method") @DSBan(DSCat.PRIVATE_METHOD) @DSGenerator( tool_name = "Doppelganger", tool_version = "2.0", generated_on = "2013-12-30 12:32:41.967 -0500", hash_original_method = "D5A9FC7DDB356B7F9D175C82B50A2AF1", hash_generated_method = "24C7574FF957618A543446AAAEE4BC4D") private void setCookies(String url, String value) { if (value.contains("\r") || value.contains("\n")) { // for security reason, filter out '\r' and '\n' from the cookie int size = value.length(); StringBuilder buffer = new StringBuilder(size); int i = 0; while (i != -1 && i < size) { int ir = value.indexOf('\r', i); int in = value.indexOf('\n', i); int newi = (ir == -1) ? in : (in == -1 ? ir : (ir < in ? ir : in)); if (newi > i) { buffer.append(value.subSequence(i, newi)); } else if (newi == -1) { buffer.append(value.subSequence(i, size)); break; } i = newi + 1; } value = buffer.toString(); } CookieManager.getInstance().setCookie(url, value); }
private void syncFromRamToFlash(ArrayList<Cookie> list) { Iterator<Cookie> iter = list.iterator(); while (iter.hasNext()) { Cookie cookie = iter.next(); if (cookie.mode != Cookie.MODE_NORMAL) { if (cookie.mode != Cookie.MODE_NEW) { mDataBase.deleteCookies(cookie.domain, cookie.path, cookie.name); } if (cookie.mode != Cookie.MODE_DELETED) { mDataBase.addCookie(cookie); CookieManager.getInstance().syncedACookie(cookie); } else { CookieManager.getInstance().deleteACookie(cookie); } } } }
public void testRemoveCookie() throws Exception { man.setThreadContext(jmctx); Cookie c = new Cookie("id", "me", "127.0.0.1", "/", false, 0); man.add(c); assertEquals(1, man.getCookieCount()); // This should be ignored, as there is no value Cookie d = new Cookie("id", "", "127.0.0.1", "/", false, 0); man.add(d); assertEquals(0, man.getCookieCount()); man.add(c); man.add(c); assertEquals(1, man.getCookieCount()); Cookie e = new Cookie("id", "me2", "127.0.0.1", "/", false, 0); man.add(e); assertEquals(1, man.getCookieCount()); }
/** Returns whether cookies are enabled or not. */ @DSComment("Private Method") @DSBan(DSCat.PRIVATE_METHOD) @DSGenerator( tool_name = "Doppelganger", tool_version = "2.0", generated_on = "2013-12-30 12:32:41.972 -0500", hash_original_method = "E9376C130584FE2146C1CFB7A84FB471", hash_generated_method = "FA6BBF0A4D9665939C38DB115FB61EFF") private boolean cookiesEnabled() { return CookieManager.getInstance().acceptCookie(); }
/** * Retrieve the cookie string for the given url. * * @param url The resource's url. * @return A String representing the cookies for the given resource url. */ @DSComment("Private Method") @DSBan(DSCat.PRIVATE_METHOD) @DSGenerator( tool_name = "Doppelganger", tool_version = "2.0", generated_on = "2013-12-30 12:32:41.970 -0500", hash_original_method = "E3D7D6931554145E868760CB2C4A26A3", hash_generated_method = "EE86FFF77F12F6E87C0C4F0204A00383") private String cookies(String url) { return CookieManager.getInstance().getCookie(url); }
protected void syncFromRamToFlash() { if (DebugFlags.COOKIE_SYNC_MANAGER) { Log.v(LOGTAG, "CookieSyncManager::syncFromRamToFlash STARTS"); } if (!CookieManager.getInstance().acceptCookie()) { return; } ArrayList<Cookie> cookieList = CookieManager.getInstance().getUpdatedCookiesSince(mLastUpdate); mLastUpdate = System.currentTimeMillis(); syncFromRamToFlash(cookieList); ArrayList<Cookie> lruList = CookieManager.getInstance().deleteLRUDomain(); syncFromRamToFlash(lruList); if (DebugFlags.COOKIE_SYNC_MANAGER) { Log.v(LOGTAG, "CookieSyncManager::syncFromRamToFlash DONE"); } }
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.oauthactivity_layout); ActionBar actionBar = getActionBar(); actionBar.setDisplayHomeAsUpEnabled(false); actionBar.setTitle(getString(R.string.login)); webView = (WebView) findViewById(R.id.webView); webView.setWebViewClient(new WeiboWebViewClient()); WebSettings settings = webView.getSettings(); settings.setJavaScriptEnabled(true); settings.setSaveFormData(false); settings.setSavePassword(false); settings.setCacheMode(WebSettings.LOAD_NO_CACHE); settings.setRenderPriority(WebSettings.RenderPriority.HIGH); CookieSyncManager.createInstance(this); CookieManager cookieManager = CookieManager.getInstance(); cookieManager.removeAllCookie(); }
public void testCookiePolicyNetscape() throws Exception { man.setCookiePolicy(CookiePolicy.NETSCAPE); man.testStarted(); // ensure policy is picked up URL url = new URL("http://www.order.now/sub1/moo.html"); man.addCookieFromHeader("test1=moo1;", url); man.addCookieFromHeader("test2=moo2;path=/sub1", url); man.addCookieFromHeader("test2=moo3;path=/", url); assertEquals(3, man.getCookieCount()); assertEquals("/sub1", man.get(0).getPath()); assertEquals("/sub1", man.get(1).getPath()); assertEquals("/", man.get(2).getPath()); String s = man.getCookieHeaderForURL(url); assertNotNull(s); org.apache.commons.httpclient.Cookie[] c = man.getCookiesForUrl(url); assertEquals("/sub1", c[0].getPath()); assertFalse(c[0].isPathAttributeSpecified()); assertEquals("/sub1", c[1].getPath()); assertTrue(c[1].isPathAttributeSpecified()); assertEquals("/", c[2].getPath()); assertTrue(c[2].isPathAttributeSpecified()); assertEquals("test1=moo1; test2=moo2; test2=moo3", s); }
public void testCookieOrdering2() throws Exception { URL url = new URL("http://order.now/sub1/moo.html"); man.addCookieFromHeader("test1=moo1;", url); man.addCookieFromHeader("test2=moo2;path=/sub1", url); man.addCookieFromHeader("test2=moo3;path=/", url); assertEquals(3, man.getCookieCount()); assertEquals("/sub1", man.get(0).getPath()); // Defaults to caller URL assertEquals("/sub1", man.get(1).getPath()); assertEquals("/", man.get(2).getPath()); String s = man.getCookieHeaderForURL(url); assertNotNull(s); org.apache.commons.httpclient.Cookie[] c = man.getCookiesForUrl(url); assertEquals("/sub1", c[0].getPath()); assertFalse(c[0].isPathAttributeSpecified()); assertEquals("/sub1", c[1].getPath()); assertTrue(c[1].isPathAttributeSpecified()); assertEquals("/", c[2].getPath()); assertEquals("test1=moo1; test2=moo2; test2=moo3", s); }
public void testDuplicateCookie2() throws Exception { URL url = new URL("http://a.b.c/"); man.addCookieFromHeader("test=1", url); man.addCookieFromHeader("test2=a", url); String s = man.getCookieHeaderForURL(url); assertNotNull(s); assertEquals("test=1; test2=a", s); // Assumes some kind of list is used man.addCookieFromHeader("test=2", url); man.addCookieFromHeader("test3=b", url); s = man.getCookieHeaderForURL(url); assertNotNull(s); assertEquals("test2=a; test=2; test3=b", s); // Assumes some kind of list is use // If not using a list that retains the order, then the asserts would need to change }
// Test cookie matching public void testCookieMatching() throws Exception { URL url = new URL("http://a.b.c:8080/TopDir/fred.jsp"); man.addCookieFromHeader("ID=abcd; Path=/TopDir", url); String s = man.getCookieHeaderForURL(url); assertNotNull(s); assertEquals("ID=abcd", s); url = new URL("http://a.b.c:8080/other.jsp"); s = man.getCookieHeaderForURL(url); assertNull(s); url = new URL("http://a.b.c:8080/TopDir/suub/another.jsp"); s = man.getCookieHeaderForURL(url); assertNotNull(s); url = new URL("http://a.b.c:8080/TopDir"); s = man.getCookieHeaderForURL(url); assertNotNull(s); url = new URL("http://a.b.d/"); s = man.getCookieHeaderForURL(url); assertNull(s); }
/** * Test that we won't be tricked by similar host names (this was a past bug, although it never got * reported in the bug database): */ public void testSimilarHostNames() throws Exception { URL url = new URL("http://ache.org/"); man.addCookieFromHeader("test=1", url); url = new URL("http://jakarta.apache.org/"); assertNull(man.getCookieHeaderForURL(url)); }
/** * Parse the headers sent from the server. * * @param headers gives up the HeaderGroup IMPORTANT: as this is called from network thread, can't * call native directly */ public void headers(Headers headers) { if (Config.LOGV) Log.v(LOGTAG, "LoadListener.headers"); if (mCancelled) return; mHeaders = headers; mMimeType = ""; mEncoding = ""; ArrayList<String> cookies = headers.getSetCookie(); for (int i = 0; i < cookies.size(); ++i) { CookieManager.getInstance().setCookie(mUri, cookies.get(i)); } long contentLength = headers.getContentLength(); if (contentLength != Headers.NO_CONTENT_LENGTH) { mContentLength = contentLength; } else { mContentLength = 0; } String contentType = headers.getContentType(); if (contentType != null) { parseContentTypeHeader(contentType); // If we have one of "generic" MIME types, try to deduce // the right MIME type from the file extension (if any): if (mMimeType.equalsIgnoreCase("text/plain") || mMimeType.equalsIgnoreCase("application/octet-stream")) { String newMimeType = guessMimeTypeFromExtension(); if (newMimeType != null) { mMimeType = newMimeType; } } else if (mMimeType.equalsIgnoreCase("text/vnd.wap.wml")) { // As we don't support wml, render it as plain text mMimeType = "text/plain"; } else { // XXX: Until the servers send us either correct xhtml or // text/html, treat application/xhtml+xml as text/html. // It seems that xhtml+xml and vnd.wap.xhtml+xml mime // subtypes are used interchangeably. So treat them the same. if (mMimeType.equalsIgnoreCase("application/xhtml+xml") || mMimeType.equals("application/vnd.wap.xhtml+xml")) { mMimeType = "text/html"; } } } else { /* Often when servers respond with 304 Not Modified or a Redirect, then they don't specify a MIMEType. When this occurs, the function below is called. In the case of 304 Not Modified, the cached headers are used rather than the headers that are returned from the server. */ guessMimeType(); } // is it an authentication request? boolean mustAuthenticate = (mStatusCode == HTTP_AUTH || mStatusCode == HTTP_PROXY_AUTH); // is it a proxy authentication request? boolean isProxyAuthRequest = (mStatusCode == HTTP_PROXY_AUTH); // is this authentication request due to a failed attempt to // authenticate ealier? mAuthFailed = false; // if we tried to authenticate ourselves last time if (mAuthHeader != null) { // we failed, if we must to authenticate again now and // we have a proxy-ness match mAuthFailed = (mustAuthenticate && isProxyAuthRequest == mAuthHeader.isProxy()); // if we did NOT fail and last authentication request was a // proxy-authentication request if (!mAuthFailed && mAuthHeader.isProxy()) { Network network = Network.getInstance(mContext); // if we have a valid proxy set if (network.isValidProxySet()) { /* The proxy credentials can be read in the WebCore thread */ synchronized (network) { // save authentication credentials for pre-emptive proxy // authentication network.setProxyUsername(mAuthHeader.getUsername()); network.setProxyPassword(mAuthHeader.getPassword()); } } } } // it is only here that we can reset the last mAuthHeader object // (if existed) and start a new one!!! mAuthHeader = null; if (mustAuthenticate) { if (mStatusCode == HTTP_AUTH) { mAuthHeader = parseAuthHeader(headers.getWwwAuthenticate()); } else { mAuthHeader = parseAuthHeader(headers.getProxyAuthenticate()); // if successfully parsed the header if (mAuthHeader != null) { // mark the auth-header object as a proxy mAuthHeader.setProxy(); } } } // Only create a cache file if the server has responded positively. if ((mStatusCode == HTTP_OK || mStatusCode == HTTP_FOUND || mStatusCode == HTTP_MOVED_PERMANENTLY || mStatusCode == HTTP_TEMPORARY_REDIRECT) && mNativeLoader != 0) { // Content arriving from a StreamLoader (eg File, Cache or Data) // will not be cached as they have the header: // cache-control: no-store mCacheResult = CacheManager.createCacheFile(mUrl, mStatusCode, headers, mMimeType, false); if (mCacheResult != null) { mCacheResult.encoding = mEncoding; } } sendMessageInternal(obtainMessage(MSG_CONTENT_HEADERS)); }
// Test Old cookie is not returned public void testOldCookie() throws Exception { URL url = new URL("http://a.b.c/"); man.addCookieFromHeader("test=1; expires=Mon, 01-Jan-1990 00:00:00 GMT", url); String s = man.getCookieHeaderForURL(url); assertNull(s); }
/** * Test that the cookie domain field is actually handled as browsers do (i.e.: host X matches * domain .X): */ public void testDomainHandling() throws Exception { URL url = new URL("http://jakarta.apache.org/"); man.addCookieFromHeader("test=1;domain=.jakarta.apache.org", url); assertNotNull(man.getCookieHeaderForURL(url)); }
public void testLoad() throws Exception { assertEquals(0, man.getCookieCount()); man.addFile(findTestPath("testfiles/cookies.txt")); assertEquals(3, man.getCookieCount()); int num = 0; assertEquals("name", man.get(num).getName()); assertEquals("value", man.get(num).getValue()); assertEquals("path", man.get(num).getPath()); assertEquals("domain", man.get(num).getDomain()); assertTrue(man.get(num).getSecure()); assertEquals(num, man.get(num).getExpires()); num++; assertEquals("name2", man.get(num).getName()); assertEquals("value2", man.get(num).getValue()); assertEquals("/", man.get(num).getPath()); assertEquals("", man.get(num).getDomain()); assertFalse(man.get(num).getSecure()); assertEquals(0, man.get(num).getExpires()); num++; assertEquals("a", man.get(num).getName()); assertEquals("b", man.get(num).getValue()); assertEquals("d", man.get(num).getPath()); assertEquals("c", man.get(num).getDomain()); assertTrue(man.get(num).getSecure()); assertEquals(0, man.get(num).getExpires()); // Show that maxlong now saved as 0 }
public void testCookiePolicyIgnore() throws Exception { man.setCookiePolicy(CookiePolicy.IGNORE_COOKIES); man.testStarted(); // ensure policy is picked up URL url = new URL("http://order.now/sub1/moo.html"); man.addCookieFromHeader("test1=moo1;", url); man.addCookieFromHeader("test2=moo2;path=/sub1", url); man.addCookieFromHeader("test2=moo3;path=/", url); assertEquals(0, man.getCookieCount()); // Cookies are ignored Cookie cc; cc = new Cookie("test1", "moo1", null, "/sub1", false, 0, false, false); man.add(cc); cc = new Cookie("test2", "moo2", null, "/sub1", false, 0, true, false); man.add(cc); cc = new Cookie("test3", "moo3", null, "/", false, 0, false, false); man.add(cc); assertEquals(3, man.getCookieCount()); assertEquals("/sub1", man.get(0).getPath()); assertEquals("/sub1", man.get(1).getPath()); assertEquals("/", man.get(2).getPath()); String s = man.getCookieHeaderForURL(url); assertNull(s); org.apache.commons.httpclient.Cookie[] c = man.getCookiesForUrl(url); assertEquals(0, c.length); // Cookies again ignored }