public virtual bool Match(Apache.Http.Cookie.Cookie cookie, CookieOrigin origin) { Args.NotNull(cookie, "Cookie"); Args.NotNull(origin, "Cookie origin"); string targetpath = origin.GetPath(); string topmostPath = cookie.GetPath(); if (topmostPath == null) { topmostPath = "/"; } if (topmostPath.Length > 1 && topmostPath.EndsWith("/")) { topmostPath = Sharpen.Runtime.Substring(topmostPath, 0, topmostPath.Length - 1); } bool match = targetpath.StartsWith(topmostPath); // if there is a match and these values are not exactly the same we have // to make sure we're not matcing "/foobar" and "/foo" if (match && targetpath.Length != topmostPath.Length) { if (!topmostPath.EndsWith("/")) { match = (targetpath[topmostPath.Length] == '/'); } } return(match); }
/// <summary>Set 'effective host name' as defined in RFC 2965.</summary> /// <remarks> /// Set 'effective host name' as defined in RFC 2965. /// <p> /// If a host name contains no dots, the effective host name is /// that name with the string .local appended to it. Otherwise /// the effective host name is the same as the host name. Note /// that all effective host names contain at least one dot. /// </remarks> /// <param name="origin">origin where cookie is received from or being sent to.</param> private static CookieOrigin AdjustEffectiveHost(CookieOrigin origin) { string host = origin.GetHost(); // Test if the host name appears to be a fully qualified DNS name, // IPv4 address or IPv6 address bool isLocalHost = true; for (int i = 0; i < host.Length; i++) { char ch = host[i]; if (ch == '.' || ch == ':') { isLocalHost = false; break; } } if (isLocalHost) { host += ".local"; return(new CookieOrigin(host, origin.GetPort(), origin.GetPath(), origin.IsSecure ())); } else { return(origin); } }
// AbstractCookieSpec is not thread-safe protected internal static string GetDefaultPath(CookieOrigin origin) { string defaultPath = origin.GetPath(); int lastSlashIndex = defaultPath.LastIndexOf('/'); if (lastSlashIndex >= 0) { if (lastSlashIndex == 0) { //Do not remove the very first slash lastSlashIndex = 1; } defaultPath = Sharpen.Runtime.Substring(defaultPath, 0, lastSlashIndex); } return(defaultPath); }
/// <exception cref="Apache.Http.Cookie.MalformedCookieException"></exception> public virtual void Validate(Apache.Http.Cookie.Cookie cookie, CookieOrigin origin ) { if (!Match(cookie, origin)) { throw new CookieRestrictionViolationException("Illegal path attribute \"" + cookie .GetPath() + "\". Path of origin: \"" + origin.GetPath() + "\""); } }