Exemplo n.º 1
0
        /// <summary>
        /// Helper for localizing an individual URL string for a particular langtag value
        /// and URL of the current request.
        /// </summary>
        /// <param name="url">Subject URL to be localized.</param>
        /// <param name="langtag">Language with which to localize the URL.</param>
        /// <param name="requestUrl">URL of the current HTTP request being handled.</param>
        /// <returns>
        /// String describing the new localized URL, or null if the URL was not localized,
        /// either because it was already localized, or because it is from another host, or is explicitly
        /// excluded from localization by the filter.
        /// </returns>
        protected string LocalizeUrl(HttpContextBase context, string url, string langtag, Uri requestUrl, bool incomingUrl)
        {
            // If URL is already localized...leave matched token alone.
            string urlNonlocalized;
            if (m_urlLocalizer.ExtractLangTagFromUrl(context, url, UriKind.RelativeOrAbsolute, incomingUrl, out urlNonlocalized) != null) {
                return null; } // original

            // If URL is not local (i.e. remote host)...leave matched token alone.
            if (requestUrl != null && !requestUrl.IsLocal(url)) {
                return null; } // original

            // Is URL explicitly excluded from localization?
            if (!m_urlLocalizer.FilterOutgoing(url, requestUrl)) {
                return null; } // original

            // Localize the URL.
            return m_urlLocalizer.SetLangTagInUrlPath(context, url, UriKind.RelativeOrAbsolute, langtag);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Helper for localizing an individual URL string for a particular langtag value
        /// and URL of the current request.
        /// </summary>
        /// <param name="url">Subject URL to be localized.</param>
        /// <param name="langtag">Language with which to localize the URL.</param>
        /// <param name="requestUrl">URL of the current HTTP request being handled.</param>
        /// <returns>
        /// String describing the new localized URL, or null if the URL was not localized,
        /// either because it was already localized, or because it is from another host, or is explicitly
        /// excluded from localization by the filter.
        /// </returns>
        protected string LocalizeUrl(System.Web.HttpContextBase context, string url, string langtag, Uri requestUrl, bool incomingUrl)
        {
            // If URL has prefix indicating it should be excluded from localization...return unlocalized URL
            if (url.StartsWith(IgnoreLocalizationUrlPrefix)) {
                return url.Substring(IgnoreLocalizationUrlPrefix.Length); }

            // If URL is already localized...leave matched token alone.
            string urlNonlocalized;
            if (m_urlLocalizer.ExtractLangTagFromUrl(context, url, UriKind.RelativeOrAbsolute, incomingUrl, out urlNonlocalized) != null) {
                return null; } // original

            // If URL is not local (i.e. remote host)...leave matched token alone.
            if (requestUrl != null && !requestUrl.IsLocal(url)) {
                return null; } // original

            // Is URL explicitly excluded from localization?
            if (!m_urlLocalizer.FilterOutgoing(url, requestUrl)) {
                return null; } // original

            // Localize the URL.
            return m_urlLocalizer.SetLangTagInUrlPath(context, url, UriKind.RelativeOrAbsolute, langtag);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Helper for localizing an individual URL string for a particular langtag value
        /// and URL of the current request.
        /// </summary>
        /// <param name="url">Subject URL to be localized.</param>
        /// <param name="langtag">Language with which to localize the URL.</param>
        /// <param name="requestUrl">URL of the current HTTP request being handled.</param>
        /// <returns>
        /// String describing the new localized URL, or null if the URL was not localized,
        /// either because it was already localized, or because it is from another host, or is explicitly
        /// excluded from localization by the filter.
        /// </returns>
        protected string LocalizeUrl(System.Web.HttpContextBase context, string url, string langtag, Uri requestUrl, bool incomingUrl)
        {
            // If URL has prefix indicating it should be excluded from localization...return unlocalized URL
            if (url.StartsWith(IgnoreLocalizationUrlPrefix)) {
                return url.Substring(IgnoreLocalizationUrlPrefix.Length); }

            // If URL is already localized...leave matched token alone.
            string urlNonlocalized;
            if (m_urlLocalizer.ExtractLangTagFromUrl(context, url, UriKind.RelativeOrAbsolute, incomingUrl, out urlNonlocalized) != null) {
                return null; } // original

            // If URL is not local (i.e. remote host)...leave matched token alone.
            if (requestUrl != null && !requestUrl.IsLocal(url)) {
                return null; } // original

            // Is URL explicitly excluded from localization?
            if (!m_urlLocalizer.FilterOutgoing(url, requestUrl)) {
                return null; } // original

            // If url is un-rooted...make it rooted based on any current request URL.
            // This is to resolve problems caused to i18n with resource URL paths relative to the
            // current page (see issue #286).
            // By un-rooted we mean the url is not absolute (i.e. it starts from the path component),
            // and that that path component itself is a relative path (i.e. it doesn't start with a /).
            // In doing so we also eliminate any "../..", "./", etc.
            // Examples:
            //      url (before)                            requestUrl                  url (after)
            //      -------------------------------------------------------------------------------------------------------------------
            //      content/fred.jpg                        http://example.com/blog/    /blog/content/fred.jpg              (changed)
            //      content/fred.jpg                        http://example.com/blog     /content/fred.jpg                   (changed)
            //      /content/fred.jpg                       http://example.com/blog/    /content/fred.jpg                   (unchanged)
            //      http://example.com/content/fred.jpg     http://example.com/blog/    http://example.com/content/fred.jpg (unchanged)
            // See also test cases in ResponseFilterTests.ResponseFilter_can_patch_html_urls.
            //
            if (requestUrl != null) {
                bool urlIsUnrooted = !url.StartsWith("/")
                    && (!url.Contains(":") || !Uri.IsWellFormedUriString(url, UriKind.Absolute));
                    // NB: the above is somewhat elaborate so as to avoid an object allocation
                    // in all but edge cases. Note also that Uri.IsWellFormedUriString internally
                    // simply does a Uri.TryCreate (at least as of .NET 4.6.1).
                if (urlIsUnrooted) {
                    Uri newUri = new Uri(requestUrl, url);
                    url = newUri.PathAndQuery;
                }
            }

            // Localize the URL.
            return m_urlLocalizer.SetLangTagInUrlPath(context, url, UriKind.RelativeOrAbsolute, langtag);
        }