Example #1
0
        private void HandleTokenResponse(NameValueCollection parameters, IRestResponse response)
        {
            AfterGetAccessToken(new BeforeAfterRequestArgs
            {
                Response   = response,
                Parameters = parameters
            });

            AccessToken = ParseTokenResponse(response.Content, AccessTokenKey);
            if (String.IsNullOrEmpty(AccessToken))
            {
                throw new UnexpectedResponseException(AccessTokenKey);
            }

            if (GrantType != "refresh_token")
            {
                RefreshToken = ParseTokenResponse(response.Content, RefreshTokenKey);
            }

            TokenType = ParseTokenResponse(response.Content, TokenTypeKey);

            int expiresIn;

            if (Int32Ex.TryParse(ParseTokenResponse(response.Content, ExpiresKey), out expiresIn))
            {
                ExpiresAt = DateTime.Now.AddSeconds(expiresIn);
            }
        }
        private static void removePrefix(string uriPrefix, HttpListener listener)
        {
            var pref = new HttpListenerPrefix(uriPrefix);

            var addr = convertToIPAddress(pref.Host);

            if (addr == null)
            {
                return;
            }

            if (!addr.IsLocal())
            {
                return;
            }

            int port;

#if SSHARP
            if (!Int32Ex.TryParse(pref.Port, out port))
#else
            if (!Int32.TryParse(pref.Port, out port))
#endif
            { return; }

            if (!port.IsPortNumber())
            {
                return;
            }

            var path = pref.Path;
            if (path.IndexOf('%') != -1)
            {
                return;
            }

            if (path.IndexOf("//", StringComparison.Ordinal) != -1)
            {
                return;
            }

            var endpoint = new IPEndPoint(addr, port);

            EndPointListener lsnr;
            if (!_endpoints.TryGetValue(endpoint, out lsnr))
            {
                return;
            }

            if (lsnr.IsSecure ^ pref.IsSecure)
            {
                return;
            }

            lsnr.RemovePrefix(pref, listener);
        }
        private static CookieCollection parseResponse(string value)
        {
            var ret = new CookieCollection();

            Cookie cookie = null;

            var caseInsensitive = StringComparison.InvariantCultureIgnoreCase;
            var pairs           = value.SplitHeaderValue(',', ';').ToList();

            for (var i = 0; i < pairs.Count; i++)
            {
                var pair = pairs[i].Trim();
                if (pair.Length == 0)
                {
                    continue;
                }

                var idx = pair.IndexOf('=');
                if (idx == -1)
                {
                    if (cookie == null)
                    {
                        continue;
                    }

                    if (pair.Equals("port", caseInsensitive))
                    {
                        cookie.Port = "\"\"";
                        continue;
                    }

                    if (pair.Equals("discard", caseInsensitive))
                    {
                        cookie.Discard = true;
                        continue;
                    }

                    if (pair.Equals("secure", caseInsensitive))
                    {
                        cookie.Secure = true;
                        continue;
                    }

                    if (pair.Equals("httponly", caseInsensitive))
                    {
                        cookie.HttpOnly = true;
                        continue;
                    }

                    continue;
                }

                if (idx == 0)
                {
                    if (cookie != null)
                    {
                        ret.add(cookie);
                        cookie = null;
                    }

                    continue;
                }

                var name = pair.Substring(0, idx).TrimEnd(' ');
                var val  = idx < pair.Length - 1
                                                         ? pair.Substring(idx + 1).TrimStart(' ')
                                                         : String.Empty;

                if (name.Equals("version", caseInsensitive))
                {
                    if (cookie == null)
                    {
                        continue;
                    }

                    if (val.Length == 0)
                    {
                        continue;
                    }

                    int num;
                    if (!Int32Ex.TryParse(val.Unquote(), out num))
                    {
                        continue;
                    }

                    cookie.Version = num;
                    continue;
                }

                if (name.Equals("expires", caseInsensitive))
                {
                    if (val.Length == 0)
                    {
                        continue;
                    }

                    if (i == pairs.Count - 1)
                    {
                        break;
                    }

                    i++;

                    if (cookie == null)
                    {
                        continue;
                    }

                    if (cookie.Expires != DateTime.MinValue)
                    {
                        continue;
                    }

                    var buff = new StringBuilder(val, 32);
                    buff.AppendFormat(", {0}", pairs[i].Trim());

                    DateTime expires;
                    if (!DateTimeEx.TryParseExact(
                            buff.ToString(),
                            new[] { "ddd, dd'-'MMM'-'yyyy HH':'mm':'ss 'GMT'", "r" },
                            CultureInfo.CreateSpecificCulture("en-US"),
                            DateTimeStyles.AdjustToUniversal
                            | DateTimeStyles.AssumeUniversal,
                            out expires
                            )
                        )
                    {
                        continue;
                    }

                    cookie.Expires = expires.ToLocalTime();
                    continue;
                }

                if (name.Equals("max-age", caseInsensitive))
                {
                    if (cookie == null)
                    {
                        continue;
                    }

                    if (val.Length == 0)
                    {
                        continue;
                    }

                    int num;
                    if (!Int32Ex.TryParse(val.Unquote(), out num))
                    {
                        continue;
                    }

                    cookie.setMaxAge(num);

                    continue;
                }

                if (name.Equals("path", caseInsensitive))
                {
                    if (cookie == null)
                    {
                        continue;
                    }

                    if (val.Length == 0)
                    {
                        continue;
                    }

                    cookie.Path = val;
                    continue;
                }

                if (name.Equals("domain", caseInsensitive))
                {
                    if (cookie == null)
                    {
                        continue;
                    }

                    if (val.Length == 0)
                    {
                        continue;
                    }

                    cookie.Domain = val;
                    continue;
                }

                if (name.Equals("port", caseInsensitive))
                {
                    if (cookie == null)
                    {
                        continue;
                    }

                    if (val.Length == 0)
                    {
                        continue;
                    }

                    cookie.Port = val;
                    continue;
                }

                if (name.Equals("comment", caseInsensitive))
                {
                    if (cookie == null)
                    {
                        continue;
                    }

                    if (val.Length == 0)
                    {
                        continue;
                    }

                    cookie.Comment = urlDecode(val, Encoding.UTF8);
                    continue;
                }

                if (name.Equals("commenturl", caseInsensitive))
                {
                    if (cookie == null)
                    {
                        continue;
                    }

                    if (val.Length == 0)
                    {
                        continue;
                    }

                    cookie.CommentUri = val.Unquote().ToUri();
                    continue;
                }

                if (cookie != null)
                {
                    ret.add(cookie);
                }

                cookie = new Cookie(name, val);
            }

            if (cookie != null)
            {
                ret.add(cookie);
            }

            return(ret);
        }
        private static CookieCollection parseRequest(string value)
        {
            var ret = new CookieCollection();

            Cookie cookie = null;
            var    ver    = 0;

            var caseInsensitive = StringComparison.InvariantCultureIgnoreCase;
            var pairs           = value.SplitHeaderValue(',', ';').ToList();

            for (var i = 0; i < pairs.Count; i++)
            {
                var pair = pairs[i].Trim();
                if (pair.Length == 0)
                {
                    continue;
                }

                var idx = pair.IndexOf('=');
                if (idx == -1)
                {
                    if (cookie == null)
                    {
                        continue;
                    }

                    if (pair.Equals("$port", caseInsensitive))
                    {
                        cookie.Port = "\"\"";
                        continue;
                    }

                    continue;
                }

                if (idx == 0)
                {
                    if (cookie != null)
                    {
                        ret.add(cookie);
                        cookie = null;
                    }

                    continue;
                }

                var name = pair.Substring(0, idx).TrimEnd(' ');
                var val  = idx < pair.Length - 1
                                                         ? pair.Substring(idx + 1).TrimStart(' ')
                                                         : String.Empty;

                if (name.Equals("$version", caseInsensitive))
                {
                    if (val.Length == 0)
                    {
                        continue;
                    }

                    int num;
                    if (!Int32Ex.TryParse(val.Unquote(), out num))
                    {
                        continue;
                    }

                    ver = num;
                    continue;
                }

                if (name.Equals("$path", caseInsensitive))
                {
                    if (cookie == null)
                    {
                        continue;
                    }

                    if (val.Length == 0)
                    {
                        continue;
                    }

                    cookie.Path = val;
                    continue;
                }

                if (name.Equals("$domain", caseInsensitive))
                {
                    if (cookie == null)
                    {
                        continue;
                    }

                    if (val.Length == 0)
                    {
                        continue;
                    }

                    cookie.Domain = val;
                    continue;
                }

                if (name.Equals("$port", caseInsensitive))
                {
                    if (cookie == null)
                    {
                        continue;
                    }

                    if (val.Length == 0)
                    {
                        continue;
                    }

                    cookie.Port = val;
                    continue;
                }

                if (cookie != null)
                {
                    ret.add(cookie);
                }

                cookie = new Cookie(name, val);

                if (ver != 0)
                {
                    cookie.Version = ver;
                }
            }

            if (cookie != null)
            {
                ret.add(cookie);
            }

            return(ret);
        }
        private static void addPrefix(string uriPrefix, HttpListener listener
#if SSHARP
                                      , EthernetAdapterType adapter
#endif
                                      )
        {
            var pref = new HttpListenerPrefix(uriPrefix);

            var addr = convertToIPAddress(pref.Host);

            if (!addr.IsLocal())
            {
                throw new HttpListenerException(87, "Includes an invalid host.");
            }

            int port;

#if SSHARP
            if (!Int32Ex.TryParse(pref.Port, out port))
#else
            if (!Int32.TryParse(pref.Port, out port))
#endif
            { throw new HttpListenerException(87, "Includes an invalid port."); }

            if (!port.IsPortNumber())
            {
                throw new HttpListenerException(87, "Includes an invalid port.");
            }

            var path = pref.Path;
            if (path.IndexOf('%') != -1)
            {
                throw new HttpListenerException(87, "Includes an invalid path.");
            }

            if (path.IndexOf("//", StringComparison.Ordinal) != -1)
            {
                throw new HttpListenerException(87, "Includes an invalid path.");
            }

            var endpoint = new IPEndPoint(addr, port);

            EndPointListener lsnr;
            if (_endpoints.TryGetValue(endpoint, out lsnr))
            {
                if (lsnr.IsSecure ^ pref.IsSecure)
                {
                    throw new HttpListenerException(87, "Includes an invalid scheme.");
                }
            }
            else
            {
                lsnr = new EndPointListener(endpoint, pref.IsSecure, listener.CertificateFolderPath,
#if !NETCF || BCC || SSL
                                            listener.SslConfiguration,
#endif
                                            listener.ReuseAddress,
#if SSHARP
                                            adapter,
#endif
                                            listener.Log);

                _endpoints.Add(endpoint, lsnr);
            }

            lsnr.AddPrefix(pref, listener);
        }