Esempio n. 1
0
        public static XrdDocument FetchXRD(Uri location)
        {
            HttpWebResponse response;
            Uri             xrdUrl    = null;
            MemoryStream    xrdStream = null;

            try
            {
                using (MemoryStream stream = FetchWebDocument(location, XRD_AND_HTML_TYPES, out response))
                {
                    if (stream != null)
                    {
                        if (IsXrdDocument(response.ContentType.ToLowerInvariant(), stream))
                        {
                            // We fetched an XRD document directly, skip ahead
                            xrdUrl    = location;
                            xrdStream = stream;

                            response.Close();
                        }
                        else
                        {
                            #region LRDD

                            // 1. Check the HTTP headers for Link: <...>; rel="describedby"; ...
                            xrdUrl = FindXrdDocumentLocationInHeaders(response.Headers);

                            // 2. Check the document body for <link rel="describedby" ...>
                            if (xrdUrl == null)
                            {
                                xrdUrl = FindXrdDocumentLocationInHtmlMetaTags(stream.GetStreamString());
                            }

                            // 3. TODO: Try and grab the /host-meta document
                            if (xrdUrl == null)
                            {
                                xrdUrl = FindXrdDocumentLocationFromHostMeta(new Uri(location, "/host-meta"));
                            }

                            response.Close();

                            // 4. Fetch the XRD document
                            if (xrdUrl != null)
                            {
                                xrdStream = FetchWebDocument(xrdUrl, XRD_TYPES, out response);

                                if (!IsXrdDocument(response.ContentType.ToLowerInvariant(), xrdStream))
                                {
                                    m_log.Error("XRD fetch from " + xrdUrl + " failed");
                                    xrdStream = null;
                                }

                                response.Close();
                            }

                            #endregion LRDD
                        }

                        if (xrdStream != null)
                        {
                            XrdParser   parser = new XrdParser(xrdStream);
                            XrdDocument doc    = parser.Document;
                            xrdStream.Dispose();
                            return(doc);
                        }
                    }
                    else
                    {
                        m_log.Warn("XRD discovery on endpoint " + location + " failed");
                    }
                }
            }
            catch (XrdParseException ex)
            {
                m_log.Warn("Failed to parse XRD document at " + location + ": " + ex.Message);
            }

            return(null);
        }
Esempio n. 2
0
        public static XrdDocument FetchXRD(Uri location)
        {
            HttpWebResponse response;
            Uri xrdUrl = null;
            MemoryStream xrdStream = null;

            try
            {
                using (MemoryStream stream = FetchWebDocument(location, XRD_AND_HTML_TYPES, out response))
                {
                    if (stream != null)
                    {
                        if (IsXrdDocument(response.ContentType.ToLowerInvariant(), stream))
                        {
                            // We fetched an XRD document directly, skip ahead
                            xrdUrl = location;
                            xrdStream = stream;

                            response.Close();
                        }
                        else
                        {
                            #region LRDD

                            // 1. Check the HTTP headers for Link: <...>; rel="describedby"; ...
                            xrdUrl = FindXrdDocumentLocationInHeaders(response.Headers);

                            // 2. Check the document body for <link rel="describedby" ...>
                            if (xrdUrl == null)
                                xrdUrl = FindXrdDocumentLocationInHtmlMetaTags(stream.GetStreamString());

                            // 3. TODO: Try and grab the /host-meta document
                            if (xrdUrl == null)
                                xrdUrl = FindXrdDocumentLocationFromHostMeta(new Uri(location, "/host-meta"));

                            response.Close();

                            // 4. Fetch the XRD document
                            if (xrdUrl != null)
                            {
                                xrdStream = FetchWebDocument(xrdUrl, XRD_TYPES, out response);

                                if (!IsXrdDocument(response.ContentType.ToLowerInvariant(), xrdStream))
                                {
                                    m_log.Error("XRD fetch from " + xrdUrl + " failed");
                                    xrdStream = null;
                                }

                                response.Close();
                            }

                            #endregion LRDD
                        }

                        if (xrdStream != null)
                        {
                            XrdParser parser = new XrdParser(xrdStream);
                            XrdDocument doc = parser.Document;
                            xrdStream.Dispose();
                            return doc;
                        }
                    }
                    else
                    {
                        m_log.Warn("XRD discovery on endpoint " + location + " failed");
                    }
                }
            }
            catch (XrdParseException ex)
            {
                m_log.Warn("Failed to parse XRD document at " + location + ": " + ex.Message);
            }

            return null;
        }