public IList <Square.OkHttp3.Cookie> LoadForRequest(Square.OkHttp3.HttpUrl p0)
        {
            var cookies = new List <Square.OkHttp3.Cookie>();

            var nc = CookieStore.Get(p0.Uri());

            foreach (var cookie in nc)
            {
                if (Utility.PathMatches(p0.EncodedPath(), cookie.Path))
                {
                    var builder = new Square.OkHttp3.Cookie.Builder();
                    builder.Name(cookie.Name)
                    .Value(cookie.Value)
                    .Domain(cookie.Domain)
                    .Path(cookie.Path);
                    if (cookie.Secure)
                    {
                        builder.Secure();
                    }

                    cookies.Add(builder.Build());
                }
            }

            return(cookies);
        }
Пример #2
0
        protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            // Disable caching
            if (this.DisableCaching)
            {
                var cache = new CacheControlHeaderValue();
                cache.NoCache = true;
                cache.NoStore = true;
                request.Headers.CacheControl = cache;
            }

            // Add Cookie Header if any cookie for the domain in the cookie store
            var stringBuilder = new StringBuilder();

            // check if CookieContainer is NativeCookieHandler
            var nativeCookieHandler = this.CookieContainer as NativeCookieHandler;

            if (nativeCookieHandler != null)
            {
                var cookies = nativeCookieHandler.Cookies
                              .Where(c => c.Domain == request.RequestUri.Host)
                              .Where(c => Utility.PathMatches(request.RequestUri.AbsolutePath, c.Path))
                              .ToList();;

                if (cookies != null)
                {
                    foreach (var cookie in cookies)
                    {
                        if (cookie != null)
                        {
                            stringBuilder.Append(cookie.Name + "=" + cookie.Value + ";");
                        }
                    }
                }

                var headers = request.Headers;

                foreach (var h in headers)
                {
                    if (h.Key == "Cookie")
                    {
                        foreach (var val in h.Value)
                        {
                            stringBuilder.Append(val + ";");
                        }
                    }
                }

                if (stringBuilder.Length > 0)
                {
                    request.Headers.Set("Cookie", stringBuilder.ToString().TrimEnd(';'));
                }
            }

            // Set Timeout
            if (this.Timeout != null)
            {
                var source = new CancellationTokenSource(Timeout.Value);
                cancellationToken = source.Token;
            }

            var response = await base.SendAsync(request, cancellationToken);

            // throwOnCaptiveNetwork
            if (this.throwOnCaptiveNetwork && request.RequestUri.Host != response.RequestMessage.RequestUri.Host)
            {
                throw new CaptiveNetworkException(request.RequestUri, response.RequestMessage.RequestUri);
            }

            return(response);
        }
        protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            var headers = request.Headers as IEnumerable <KeyValuePair <string, IEnumerable <string> > >;
            var ms      = new MemoryStream();

            if (request.Content != null)
            {
                await request.Content.CopyToAsync(ms).ConfigureAwait(false);

                headers = headers.Union(request.Content.Headers).ToArray();
            }

            // Add Cookie Header if any cookie for the domain in the cookie store
            var stringBuilder = new StringBuilder();

            var cookies = NSHttpCookieStorage.SharedStorage.Cookies
                          .Where(c => c.Domain == request.RequestUri.Host)
                          .Where(c => Utility.PathMatches(request.RequestUri.AbsolutePath, c.Path))
                          .ToList();

            foreach (var cookie in cookies)
            {
                stringBuilder.Append(cookie.Name + "=" + cookie.Value + ";");
            }

            var rq = new NSMutableUrlRequest()
            {
                AllowsCellularAccess = true,
                Body        = NSData.FromArray(ms.ToArray()),
                CachePolicy = (!this.DisableCaching ? NSUrlRequestCachePolicy.UseProtocolCachePolicy : NSUrlRequestCachePolicy.ReloadIgnoringCacheData),
                Headers     = headers.Aggregate(new NSMutableDictionary(), (acc, x) => {
                    if (x.Key == "Cookie")
                    {
                        foreach (var val in x.Value)
                        {
                            stringBuilder.Append(val + ";");
                        }
                    }
                    else
                    {
                        acc.Add(new NSString(x.Key), new NSString(String.Join(getHeaderSeparator(x.Key), x.Value)));
                    }

                    return(acc);
                }),
                HttpMethod = request.Method.ToString().ToUpperInvariant(),
                Url        = NSUrl.FromString(request.RequestUri.AbsoluteUri),
            };

            if (stringBuilder.Length > 0)
            {
                var copy = new NSMutableDictionary(rq.Headers);
                copy.Add(new NSString("Cookie"), new NSString(stringBuilder.ToString().TrimEnd(';')));
                rq.Headers = copy;
            }

            if (Timeout != null)
            {
                rq.TimeoutInterval = Timeout.Value.TotalSeconds;
            }

            var op = session.CreateDataTask(rq);

            cancellationToken.ThrowIfCancellationRequested();

            var ret = new TaskCompletionSource <HttpResponseMessage>();

            cancellationToken.Register(() => ret.TrySetCanceled());

            lock (inflightRequests)
            {
                inflightRequests[op] = new InflightOperation()
                {
                    FutureResponse    = ret,
                    Request           = request,
                    Progress          = getAndRemoveCallbackFromRegister(request),
                    ResponseBody      = new ByteArrayListStream(),
                    CancellationToken = cancellationToken,
                };
            }

            op.Resume();
            return(await ret.Task.ConfigureAwait(false));
        }