Ejemplo n.º 1
0
        internal static NegotiationResult Parse(string json, out string error, HubConnection hub)
        {
            error = null;

            Dictionary <string, object> response = BestHTTP.JSON.Json.Decode(json) as Dictionary <string, object>;

            if (response == null)
            {
                error = "Json decoding failed!";
                return(null);
            }

            try
            {
                NegotiationResult result = new NegotiationResult();
                object            value;
                if (response.TryGetValue("connectionId", out value))
                {
                    result.ConnectionId = value.ToString();
                }

                if (response.TryGetValue("availableTransports", out value))
                {
                    List <object> transports = value as List <object>;
                    if (transports != null)
                    {
                        List <SupportedTransport> supportedTransports = new List <SupportedTransport>(transports.Count);

                        foreach (Dictionary <string, object> transport in transports)
                        {
                            string        transportName = string.Empty;
                            List <string> transferModes = null;

                            if (transport.TryGetValue("transport", out value))
                            {
                                transportName = value.ToString();
                            }

                            if (transport.TryGetValue("transferFormats", out value))
                            {
                                List <object> transferFormats = value as List <object>;

                                if (transferFormats != null)
                                {
                                    transferModes = new List <string>(transferFormats.Count);
                                    foreach (var mode in transferFormats)
                                    {
                                        transferModes.Add(mode.ToString());
                                    }
                                }
                            }

                            supportedTransports.Add(new SupportedTransport(transportName, transferModes));
                        }

                        result.SupportedTransports = supportedTransports;
                    }
                }

                if (response.TryGetValue("url", out value))
                {
                    string uriStr = value.ToString();

                    Uri redirectUri;

                    // Here we will try to parse the received url. If TryCreate fails, we will throw an exception
                    //  as it should be able to successfully parse whole (absolute) urls (like "https://server:url/path")
                    //  and relative ones (like "/path").
                    if (!Uri.TryCreate(uriStr, UriKind.RelativeOrAbsolute, out redirectUri))
                    {
                        throw new Exception(string.Format("Couldn't parse url: '{0}'", uriStr));
                    }

                    // If received a relative url we will use the hub's current url to append the new path to it.
                    if (!redirectUri.IsAbsoluteUri)
                    {
                        Uri oldUri  = hub.Uri;
                        var builder = new UriBuilder(oldUri);
                        builder.Path = uriStr;

                        redirectUri = builder.Uri;
                    }

                    result.Url = redirectUri;
                }

                if (response.TryGetValue("accessToken", out value))
                {
                    result.AccessToken = value.ToString();
                }
                else if (hub.NegotiationResult != null)
                {
                    result.AccessToken = hub.NegotiationResult.AccessToken;
                }

                return(result);
            }
            catch (Exception ex)
            {
                error = "Error while parsing result: " + ex.Message + " StackTrace: " + ex.StackTrace;
                return(null);
            }
        }
Ejemplo n.º 2
0
        internal static NegotiationResult Parse(HTTPResponse resp, out string error, HubConnection hub)
        {
            error = null;

            Dictionary <string, object> response = BestHTTP.JSON.Json.Decode(resp.DataAsText) as Dictionary <string, object>;

            if (response == null)
            {
                error = "Json decoding failed!";
                return(null);
            }

            try
            {
                NegotiationResult result = new NegotiationResult();
                result.NegotiationResponse = resp;

                object value;

                if (response.TryGetValue("negotiateVersion", out value))
                {
                    int version;
                    if (int.TryParse(value.ToString(), out version))
                    {
                        result.NegotiateVersion = version;
                    }
                }

                if (response.TryGetValue("connectionId", out value))
                {
                    result.ConnectionId = value.ToString();
                }

                if (response.TryGetValue("connectionToken", out value))
                {
                    result.ConnectionToken = value.ToString();
                }

                if (response.TryGetValue("availableTransports", out value))
                {
                    List <object> transports = value as List <object>;
                    if (transports != null)
                    {
                        List <SupportedTransport> supportedTransports = new List <SupportedTransport>(transports.Count);

                        foreach (Dictionary <string, object> transport in transports)
                        {
                            string        transportName = string.Empty;
                            List <string> transferModes = null;

                            if (transport.TryGetValue("transport", out value))
                            {
                                transportName = value.ToString();
                            }

                            if (transport.TryGetValue("transferFormats", out value))
                            {
                                List <object> transferFormats = value as List <object>;

                                if (transferFormats != null)
                                {
                                    transferModes = new List <string>(transferFormats.Count);
                                    foreach (var mode in transferFormats)
                                    {
                                        transferModes.Add(mode.ToString());
                                    }
                                }
                            }

                            supportedTransports.Add(new SupportedTransport(transportName, transferModes));
                        }

                        result.SupportedTransports = supportedTransports;
                    }
                }

                if (response.TryGetValue("url", out value))
                {
                    string uriStr = value.ToString();

                    Uri redirectUri;

                    // Here we will try to parse the received url. If TryCreate fails, we will throw an exception
                    //  as it should be able to successfully parse whole (absolute) urls (like "https://server:url/path")
                    //  and relative ones (like "/path").
                    if (!Uri.TryCreate(uriStr, UriKind.RelativeOrAbsolute, out redirectUri))
                    {
                        throw new Exception(string.Format("Couldn't parse url: '{0}'", uriStr));
                    }

                    HTTPManager.Logger.Verbose("NegotiationResult", string.Format("Parsed url({0}) into uri({1}). uri.IsAbsoluteUri: {2}, IsAbsolute: {3}", uriStr, redirectUri, redirectUri.IsAbsoluteUri, IsAbsolute(uriStr)));

                    // If received a relative url we will use the hub's current url to append the new path to it.
                    if (!IsAbsolute(uriStr))
                    {
                        Uri oldUri  = hub.Uri;
                        var builder = new UriBuilder(oldUri);

                        // ?, /
                        var pathAndQuery = uriStr.Split(new string[] { "?", "%3F", "%3f", "/", "%2F", "%2f" }, StringSplitOptions.RemoveEmptyEntries);

                        if (pathAndQuery.Length > 1)
                        {
                            builder.Query = pathAndQuery[1];
                        }
                        else
                        {
                            builder.Query = string.Empty;
                        }

                        builder.Path = pathAndQuery[0];

                        redirectUri = builder.Uri;
                    }

                    result.Url = redirectUri;
                }

                if (response.TryGetValue("accessToken", out value))
                {
                    result.AccessToken = value.ToString();
                }
                else if (hub.NegotiationResult != null)
                {
                    result.AccessToken = hub.NegotiationResult.AccessToken;
                }

                return(result);
            }
            catch (Exception ex)
            {
                error = "Error while parsing result: " + ex.Message + " StackTrace: " + ex.StackTrace;
                return(null);
            }
        }
Ejemplo n.º 3
0
        internal static NegotiationResult Parse(string json, out string error)
        {
            error = null;

            Dictionary <string, object> response = BestHTTP.JSON.Json.Decode(json) as Dictionary <string, object>;

            if (response == null)
            {
                error = "Json decoding failed!";
                return(null);
            }

            try
            {
                NegotiationResult result = new NegotiationResult();
                object            value;
                if (response.TryGetValue("connectionId", out value))
                {
                    result.ConnectionId = value.ToString();
                }

                if (response.TryGetValue("availableTransports", out value))
                {
                    List <object> transports = value as List <object>;
                    if (transports != null)
                    {
                        List <SupportedTransport> supportedTransports = new List <SupportedTransport>(transports.Count);

                        foreach (Dictionary <string, object> transport in transports)
                        {
                            string        transportName = string.Empty;
                            List <string> transferModes = null;

                            if (transport.TryGetValue("transport", out value))
                            {
                                transportName = value.ToString();
                            }

                            if (transport.TryGetValue("transferFormats", out value))
                            {
                                List <object> transferFormats = value as List <object>;

                                if (transferFormats != null)
                                {
                                    transferModes = new List <string>(transferFormats.Count);
                                    foreach (var mode in transferFormats)
                                    {
                                        transferModes.Add(mode.ToString());
                                    }
                                }
                            }

                            supportedTransports.Add(new SupportedTransport(transportName, transferModes));
                        }

                        result.SupportedTransports = supportedTransports;
                    }
                }

                if (response.TryGetValue("url", out value))
                {
                    result.Url = new Uri(value.ToString());
                }

                if (response.TryGetValue("accessToken", out value))
                {
                    result.AccessToken = value.ToString();
                }

                return(result);
            }
            catch (Exception ex)
            {
                error = "Error while parsing result: " + ex.Message + " StackTrace: " + ex.StackTrace;
                return(null);
            }
        }