private void Insert(DreamCookie updatedCookie, string[] segments, int depth) { // find leaf node if (depth < segments.Length) { if (_jars == null) { _jars = new Dictionary <string, DreamCookieJar>(StringComparer.OrdinalIgnoreCase); } DreamCookieJar subjar; if (!_jars.TryGetValue(segments[depth], out subjar)) { subjar = new DreamCookieJar(); _jars.Add(segments[depth], subjar); } subjar.Insert(updatedCookie, segments, depth + 1); } else { if (_cookies == null) { _cookies = new List <DreamCookie>(); } List <DreamCookie> expired = new List <DreamCookie>(); for (int i = 0; i < _cookies.Count; ++i) { DreamCookie cookie = _cookies[i]; // check if cookie is expired; if so, remove it if (cookie.Expired) { expired.Add(cookie); continue; } // TODO (steveb): we need to add support for '.' prefixes on the domain name // check if cookie matches the expired cookie if (StringUtil.EqualsInvariantIgnoreCase(cookie.Domain, updatedCookie.Domain) && StringUtil.EqualsInvariantIgnoreCase(cookie.Name, updatedCookie.Name) && (cookie.Secure == updatedCookie.Secure)) { _cookies[i] = updatedCookie; return; } } foreach (DreamCookie cookie in expired) { _cookies.Remove(cookie); } _cookies.Add(updatedCookie); } }
public void Fetch_cookie_from_jar() { DreamCookie setcookie = DreamCookie.NewSetCookie("test", "123", new XUri("http://bar.com/foo")); List<DreamCookie> cookies = new List<DreamCookie>(); cookies.Add(setcookie); DreamCookieJar jar = new DreamCookieJar(); jar.Update(cookies, null); List<DreamCookie> cookies2 = jar.Fetch(new XUri("http://bar.com/foo/baz")); Assert.AreEqual(1, cookies2.Count); Assert.AreEqual("/foo", cookies2[0].Path); Assert.AreEqual("bar.com", cookies2[0].Domain); }
public void Set_Fetch_cookie_from_jar_for_https() { List<DreamCookie> setcookies = new List<DreamCookie>(); setcookies.Add(DreamCookie.NewSetCookie("authtoken", "1_633698885517217440_64e1d64e732341bde1797f20fe2ab824", new XUri("http:///"), DateTime.UtcNow.AddDays(2))); DreamCookieJar jar = new DreamCookieJar(); jar.Update(setcookies,new XUri("https://*****:*****@wikiaddress//@api/deki/users/authenticate")); List<DreamCookie> cookies = jar.Fetch(new XUri("https://wikiaddress//@api/deki/Pages/home/files,subpages")); Assert.AreEqual(1,cookies.Count); Assert.AreEqual("/", cookies[0].Path); Assert.AreEqual("1_633698885517217440_64e1d64e732341bde1797f20fe2ab824", cookies[0].Value); Assert.AreEqual("authtoken", cookies[0].Name); }
public void Can_inject_cookie_strings() { var jar = new DreamCookieJar(); jar.Update(new DreamCookie("x", "xx", _plug), _plug); jar.Update(new DreamCookie("y", "yy", _plug), _plug); AssertFeature( "GET:sync/cookies/string", _plug.At("sync", "cookies", "string").WithCookieJar(jar), new XDoc("r").Elem("x", "xx").Elem("y", "yy")); }
public void Can_inject_cookie_objects_without_cookie_attribute() { var jar = new DreamCookieJar(); jar.Update(new DreamCookie("x", "xx", _plug), _plug); AssertFeature( "GET:sync/cookies/obj", _plug.At("sync", "cookies", "obj").WithCookieJar(jar), new XDoc("r").Elem("x", "xx")); }
private DreamServiceInfo(DreamServiceInfo info, DreamCookieJar cookies) { _internalSetCookie = info._internalSetCookie; _privateSetCookie = info._privateSetCookie; AtLocalHost = info.AtLocalHost.WithCookieJar(cookies); }
private DreamCookieJar GetJar(XDoc setCookieElement) { DreamCookieJar jar = new DreamCookieJar(); List<DreamCookie> collection = new List<DreamCookie>(); collection.Add(DreamCookie.ParseSetCookie(setCookieElement)); jar.Update(collection, null); return jar; }
/// <summary> /// Create a copy of the instance with an override cookie jar. /// </summary> /// <param name="cookieJar">Cookie jar to use.</param> /// <returns>New instance.</returns> public Plug WithCookieJar(DreamCookieJar cookieJar) { return new Plug(Uri, Timeout, _headers, _preHandlers, _postHandlers, Credentials, cookieJar, MaxAutoRedirects); }
//--- Constructors --- /// <summary> /// Create a new instance. /// </summary> /// <param name="uri">Uri to the resource to make the request against.</param> /// <param name="timeout">Invocation timeout.</param> /// <param name="headers">Header collection for request.</param> /// <param name="preHandlers">Optional pre-invocation handlers.</param> /// <param name="postHandlers">Optional post-invocation handlers.</param> /// <param name="credentials">Optional request credentials.</param> /// <param name="cookieJarOverride">Optional cookie jar to override global jar shared by <see cref="Plug"/> instances.</param> /// <param name="maxAutoRedirects">Maximum number of redirects to follow, 0 if non redirects should be followed.</param> public Plug(XUri uri, TimeSpan timeout, DreamHeaders headers, List<PlugHandler> preHandlers, List<PlugHandler> postHandlers, ICredentials credentials, DreamCookieJar cookieJarOverride, ushort maxAutoRedirects) { if(uri == null) { throw new ArgumentNullException("uri"); } this.Uri = uri; this.Timeout = timeout; this.Credentials = credentials; _headers = headers; _preHandlers = preHandlers; _postHandlers = postHandlers; _cookieJarOverride = cookieJarOverride; _maxAutoRedirects = maxAutoRedirects; }
private static DreamMessage PostProcess(string verb, XUri uri, XUri normalizedUri, DreamHeaders headers, DreamCookieJar cookies, DreamMessage message) { // check if we received cookies if(message.HasCookies) { // add matching cookies to service or to global cookie jar if(cookies != null) { lock(cookies) { if(!StringUtil.EqualsInvariant(uri.Scheme, "local") && StringUtil.EqualsInvariant(normalizedUri.Scheme, "local")) { // need to translate cookies as they leave the dreamcontext cookies.Update(DreamCookie.ConvertToPublic(message.Cookies), uri); } else { cookies.Update(message.Cookies, uri); } } } } return message; }
private static DreamMessage PreProcess(string verb, XUri uri, XUri normalizedUri, DreamHeaders headers, DreamCookieJar cookies, DreamMessage message) { // check if plug is running in the context of a service DreamContext context = DreamContext.CurrentOrNull; if(context != null) { // set request id header message.Headers.DreamRequestId = context.GetState<string>(DreamHeaders.DREAM_REQUEST_ID); // set dream service header if(context.Service.Self != null) { message.Headers.DreamService = context.AsPublicUri(context.Service.Self).ToString(); } // check if uri is local:// if(normalizedUri.Scheme.EqualsInvariant("local")) { DreamUtil.AppendHeadersToInternallyForwardedMessage(context.Request, message); } } if(cookies != null) { lock(cookies) { message.Cookies.AddRange(cookies.Fetch(uri)); } } // transfer plug headers message.Headers.AddRange(headers); return message; }
public void With_and_without_cookiejar() { DreamCookie global = new DreamCookie("test", "global", new XUri("http://baz.com/foo")); List<DreamCookie> globalCollection = new List<DreamCookie>(); globalCollection.Add(global); Plug.GlobalCookies.Update(globalCollection, null); DreamCookie local = new DreamCookie("test", "local", new XUri("http://baz.com/foo")); List<DreamCookie> localCollection = new List<DreamCookie>(); localCollection.Add(local); DreamCookieJar localJar = new DreamCookieJar(); localJar.Update(localCollection, null); Plug globalPlug = Plug.New("http://baz.com/foo/bar"); Plug localPlug = globalPlug.WithCookieJar(localJar); Plug globalPlug2 = localPlug.WithoutCookieJar(); Assert.AreEqual("global", globalPlug.CookieJar.Fetch(globalPlug.Uri)[0].Value); Assert.AreEqual("local", localPlug.CookieJar.Fetch(localPlug.Uri)[0].Value); Assert.AreEqual("global", globalPlug2.CookieJar.Fetch(globalPlug2.Uri)[0].Value); }
public void Can_specify_private_key_for_service() { var key = StringUtil.CreateAlphaNumericKey(4); var service = _hostInfo.CreateService(typeof(AccessTestService), "customkeys", new XDoc("config").Elem("private-service-key", key)); var keys = service.AtLocalHost.At("keys").Get().ToDocument(); Assert.AreEqual(key, keys["private-service-key"].AsText, "private service key was wrong"); Assert.AreNotEqual(key, keys["internal-service-key"].AsText, "internal service key should not be the same as private key"); var cookiejar = new DreamCookieJar(); cookiejar.Update(DreamCookie.NewSetCookie("service-key", key, service.AtLocalHost.Uri), service.AtLocalHost.Uri); service.WithoutKeys().AtLocalHost .WithCookieJar(cookiejar) .At("private") .Get(new Result<DreamMessage>()) .Wait() .AssertSuccess("access with private key failed"); }
private void Insert(DreamCookie updatedCookie, string[] segments, int depth) { // find leaf node if(depth < segments.Length) { if(_jars == null) { _jars = new Dictionary<string, DreamCookieJar>(StringComparer.OrdinalIgnoreCase); } DreamCookieJar subjar; if(!_jars.TryGetValue(segments[depth], out subjar)) { subjar = new DreamCookieJar(); _jars.Add(segments[depth], subjar); } subjar.Insert(updatedCookie, segments, depth + 1); } else { if(_cookies == null) { _cookies = new List<DreamCookie>(); } List<DreamCookie> expired = new List<DreamCookie>(); for(int i = 0; i < _cookies.Count; ++i) { DreamCookie cookie = _cookies[i]; // check if cookie is expired; if so, remove it if(cookie.Expired) { expired.Add(cookie); continue; } // TODO (steveb): we need to add support for '.' prefixes on the domain name // check if cookie matches the expired cookie if(StringUtil.EqualsInvariantIgnoreCase(cookie.Domain, updatedCookie.Domain) && StringUtil.EqualsInvariantIgnoreCase(cookie.Name, updatedCookie.Name) && (cookie.Secure == updatedCookie.Secure)) { _cookies[i] = updatedCookie; return; } } foreach(DreamCookie cookie in expired) { _cookies.Remove(cookie); } _cookies.Add(updatedCookie); } }