Exemplo n.º 1
0
        /*
         * private enum _atomType
         * {
         *  atomFeed,
         *  atomPub,
         *  atomAPI,
         *  atomGData
         * }
         */

        public ServiceDiscovery()
        {
            _httpClient = new HttpClient();

            _serviceDocKind = _serviceDocumentKind.Unknown;

            _serviceTypes = ServiceTypes.Unknown;
        }
Exemplo n.º 2
0
        private async Task <bool> ParseHTML(HttpContent content)
        {
            UpdateStatus(">> Trying to parse a HTML document...");

            //Stream st = content.ReadAsStreamAsync().Result;

            string s = await content.ReadAsStringAsync();

            UpdateStatus(">> Loading a HTML document...");

            mshtml.HTMLDocument   hd   = new mshtml.HTMLDocument();
            mshtml.IHTMLDocument2 hdoc = (mshtml.IHTMLDocument2)hd;

            IPersistStreamInit ips = (IPersistStreamInit)hdoc;

            ips.InitNew();

            hdoc.designMode = "On";
            hdoc.clear();
            hdoc.write(s);
            hdoc.close();

            // In Delphi, it's just
            // hdoc:= CreateComObject(Class_HTMLDocument) as IHTMLDocument2;
            // (hdoc as IPersistStreamInit).Load(TStreamAdapter.Create(Response.Stream));

            int i = 0;

            while (hdoc.readyState != "complete")
            {
                await Task.Delay(100);

                if (i > 500)
                {
                    throw new Exception(string.Format("The document {0} timed out while loading", "IHTMLDocument2"));
                }
                i++;
            }

            if (hdoc.readyState == "complete")
            {
                UpdateStatus(">> Checking HTML link tags...");

                IHTMLElementCollection ElementCollection = hdoc.all;
                foreach (var e in ElementCollection)
                {
                    if ((e as IHTMLElement).tagName == "LINK")
                    {
                        string re = (e as IHTMLElement).getAttribute("rel", 0);
                        if (!string.IsNullOrEmpty(re))
                        {
                            if (re.ToUpper() == "EDITURI")
                            {
                                string hf = (e as IHTMLElement).getAttribute("href", 0);
                                if (!string.IsNullOrEmpty(hf))
                                {
                                    _serviceDocKind = _serviceDocumentKind.RSD;
                                    _serviceDocUrl  = hf;

                                    Debug.WriteLine("ServiceDocumentKind is: RSD " + _serviceDocUrl);

                                    UpdateStatus("Found a link to a RSD documnet.");
                                }
                            }
                            else if (re.ToUpper() == "SERVICE")
                            {
                                string ty = (e as IHTMLElement).getAttribute("type", 0);
                                if (!string.IsNullOrEmpty(ty))
                                {
                                    if (ty == "application/atomsvc+xml")
                                    {
                                        string hf = (e as IHTMLElement).getAttribute("href", 0);
                                        if (!string.IsNullOrEmpty(hf))
                                        {
                                            _serviceDocKind = _serviceDocumentKind.AtomSrv;
                                            _serviceDocUrl  = hf;

                                            Debug.WriteLine("ServiceDocumentKind is: AtomSrv " + _serviceDocUrl);

                                            UpdateStatus("Found a link to an Atom service documnet.");
                                        }
                                    }
                                }
                            }
                            else if (re.ToUpper() == "ALTERNATE")
                            {
                                string ty = (e as IHTMLElement).getAttribute("type", 0);
                                if (!string.IsNullOrEmpty(ty))
                                {
                                    if (ty == "application/atom+xml")
                                    {
                                        string hf = (e as IHTMLElement).getAttribute("href", 0);
                                        if (!string.IsNullOrEmpty(hf))
                                        {
                                            Debug.WriteLine("Atom feed found.");
                                            try
                                            {
                                                _atomFeedUrl = new Uri(hf);
                                            }
                                            catch { }

                                            UpdateStatus("Found a link to an Atom feed.");
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return(true);
        }
Exemplo n.º 3
0
        public async Task <ServiceResultBase> DiscoverService(Uri addr)
        {
            // Initialize variables.
            _serviceDocKind = _serviceDocumentKind.Unknown;
            _serviceDocUrl  = null;
            _endpointUrl    = null;
            _atomFeedUrl    = null;
            _blogId         = "";
            _serviceTypes   = ServiceTypes.Unknown;

            UpdateStatus(">> Trying to access given URL...");

            var HTTPResponse = await _httpClient.GetAsync(addr);

            if (HTTPResponse.IsSuccessStatusCode)
            {
                if (HTTPResponse.Content == null)
                {
                    UpdateStatus("<< Received no content.");
                    ServiceResultErr re = new ServiceResultErr("Did not return any content. Content empty.");
                    return(re);
                }

                string contenTypeString = HTTPResponse.Content.Headers.GetValues("Content-Type").FirstOrDefault();

                if (!string.IsNullOrEmpty(contenTypeString))
                {
                    Debug.WriteLine("GET Content-Type header is: " + contenTypeString);

                    if (contenTypeString.StartsWith("text/html"))
                    {
                        UpdateStatus("<< Returned a HTML webpage.");

                        bool x = await ParseHTML(HTTPResponse.Content);

                        if (_serviceDocKind == _serviceDocumentKind.AtomSrv)
                        {
                            ServiceResultAtomPub ap = new ServiceResultAtomPub();
                            ap.EndpointUri = new Uri(_serviceDocUrl);
                            ap.Service     = ServiceTypes.AtomPub;
                            return(ap);
                        }
                        else if (_serviceDocKind == _serviceDocumentKind.RSD)
                        {
                            bool y = await GetRSD();

                            if ((_serviceTypes == ServiceTypes.XmlRpc_WordPress) ||
                                (_serviceTypes == ServiceTypes.XmlRpc_MovableType) &&
                                (_endpointUrl != null))
                            {
                                ServiceResultXmlRpc xp = new ServiceResultXmlRpc();
                                xp.Service     = _serviceTypes;
                                xp.EndpointUri = _endpointUrl;
                                xp.BlogID      = _blogId;
                                return(xp);
                            }
                            else
                            {
                                UpdateStatus("Could not determin service type. [WordPress,MovableType] not found.");
                                ServiceResultErr re = new ServiceResultErr("Could not determin service type.");
                                return(re);
                            }
                        }
                        else
                        {
                            UpdateStatus("Could not find any service document from HTML webpage.");

                            if (_atomFeedUrl != null)
                            {
                                UpdateStatus("Atom feed link is present.");

                                ServiceResultAtomFeed ap = new ServiceResultAtomFeed(_atomFeedUrl);
                                return(ap);
                            }
                            else
                            {
                                UpdateStatus(">> Did not find a service document link.");

                                // Could be xml-rpc endpoint.


                                // TODO:
                                // A user entered xml-rpc endpoint.
                                // Try POST some method.
                                UpdateStatus("TODO: Could not determine API from the HTML webpage.");
                                // For now.
                                ServiceResultErr re = new ServiceResultErr("Could not determine API from the HTML webpage.");
                                return(re);

                                //UpdateStatus(">> Trying to test a few things...");
                            }
                        }
                    }
                    else if (contenTypeString.StartsWith("application/atomsvc+xml"))
                    {
                        // This is the AtomPub endpoint.

                        UpdateStatus("Found an Atom Publishing Protocol service document.");

                        ServiceResultAtomPub ap = new ServiceResultAtomPub();
                        ap.EndpointUri = addr;
                        ap.Service     = ServiceTypes.AtomPub;;
                        return(ap);
                    }
                    else if (contenTypeString.StartsWith("application/rsd+xml"))
                    {
                        bool y = await GetRSD();

                        if (((_serviceTypes == ServiceTypes.XmlRpc_WordPress) || (_serviceTypes == ServiceTypes.XmlRpc_MovableType)) &&
                            (_endpointUrl != null))
                        {
                            ServiceResultXmlRpc xp = new ServiceResultXmlRpc();
                            xp.Service     = _serviceTypes;
                            xp.EndpointUri = _endpointUrl;
                            xp.BlogID      = _blogId;
                            return(xp);
                        }
                        else
                        {
                            UpdateStatus("Could not determin service type. [WordPress,MovableType] not found.");
                            ServiceResultErr re = new ServiceResultErr("Could not determin service type.");
                            return(re);
                        }
                    }
                    else if (contenTypeString.StartsWith("application/atom+xml"))
                    {
                        // TODO:
                        // Possibly AtomApi endopoint. Or Atom Feed...

                        UpdateStatus("<< Atom format returned...");

                        ServiceResultAtomFeed ap = new ServiceResultAtomFeed(addr);
                        return(ap);
                    }
                    else if (contenTypeString.StartsWith("application/x.atom+xml"))
                    {
                        // TODO:
                        // Possibly AtomApi endopoint.
                        UpdateStatus("<< Old Atom format returned... ");

                        ServiceResultAtomAPI ap = new ServiceResultAtomAPI();
                        ap.EndpointUri = addr;
                        ap.Service     = ServiceTypes.AtomApi;
                        return(ap);
                    }
                    else
                    {
                        UpdateStatus("<< Unknown Content-Type returned. " + contenTypeString + " is not supported.");
                        ServiceResultErr re = new ServiceResultErr("Content-Type unknown.");
                        return(re);
                    }
                }
                else
                {
                    UpdateStatus("<< No Content-Type returned. ");
                    ServiceResultErr re = new ServiceResultErr("Content-Type did not match.");
                    return(re);
                }
            }
            else
            {
                UpdateStatus("<< HTTP error: " + HTTPResponse.StatusCode.ToString());
                UpdateStatus("Could not retrieve any service document. ");

                //TODO: If 401 Unauthorized,
                // A user may or may not enter an AtomPub endpoint which require auth to get service document.

                ServiceResultAuthRequired rea = new ServiceResultAuthRequired(addr);
                return(rea);
            }

            //UpdateStatus(Environment.NewLine + "Finished.");
            //Debug.WriteLine("EndpointUri: " + _endpointUrl + " Service: " + _serviceTypes.ToString());
        }