Beispiel #1
0
        /// <summary>
        /// Finds a localized file resource as a best match for the language information
        /// specified in the given environment context.
        /// </summary>
        /// <param name="env">The environment data with information about preferred languages</param>
        /// <param name="path">The file path of a resource</param>
        /// <returns>
        /// The complete file path to a localized version of the resource. If there is not any
        /// localized version available, then the complete path to the original resource is returned.
        /// </returns>
        public static string LocalizePath(IEnvironment env, string path)
        {
            if (env.LanguageInfo != null)
            {
                // Try to find a file that is localized for one of the accepted languages
                foreach (CultureInfo language in env.LanguageInfo)
                {
                    string localizedPath;
                    if (CheckPathExists(path, language.Name, out localizedPath))
                    {
                        return(localizedPath);
                    }

                    // Check language code without country code
                    if (language.TwoLetterISOLanguageName != language.Name)
                    {
                        if (CheckPathExists(path, language.TwoLetterISOLanguageName, out localizedPath))
                        {
                            return(localizedPath);
                        }
                    }
                }
            }
            return(WebApp.GetPath(path));
        }
Beispiel #2
0
        private static bool CheckPathExists(string path, string language, out string localizedPath)
        {
            string languagePath;

            if (m_PathCache.TryGet(path, language, out languagePath))
            {
                localizedPath = WebApp.GetPath(languagePath);
                return(true);
            }
            else if (language != m_FallbackLanguage.Name)
            {
                languagePath  = Path.Combine(language, path);
                localizedPath = WebApp.GetPath(languagePath);
                if (File.Exists(localizedPath))
                {
                    //m_log.DebugFormat("[Wifi]: L10n for {0} results in path: {1}", language, localizedPath);
                    m_PathCache.AddOrUpdate(path, language, languagePath);
                    return(true);
                }
            }
            else
            {
                localizedPath = WebApp.GetPath(path);
                //m_log.DebugFormat("[Wifi]: L10n for {0} results in path: {1}", language, localizedPath);
                m_PathCache.AddOrUpdate(path, language, path);
                return(true);
            }
            return(false);
        }
        public override byte[] Handle(string path, Stream requestData,
                                      IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
        {
            // path = /wifi/...
            //m_log.DebugFormat("[Wifi]: path = {0}", path);
            //m_log.DebugFormat("[Wifi]: ip address = {0}", httpRequest.RemoteIPEndPoint);
            //foreach (object o in httpRequest.Query.Keys)
            //    m_log.DebugFormat("  >> {0}={1}", o, httpRequest.Query[o]);

            string resource = GetParam(path);

            resource = Uri.UnescapeDataString(resource).Trim(WebAppUtils.DirectorySeparatorChars);
            //m_log.DebugFormat("[Wifi]: resource {0}", resource);

            Request     request = RequestFactory.CreateRequest(resource, httpRequest, Localization.GetLanguageInfo(httpRequest.Headers.Get("accept-language")));
            Environment env     = new Environment(request);

            if (resource == string.Empty || resource.StartsWith("index."))
            {
                if (m_WebApp.StatisticsUpdateInterval != TimeSpan.Zero)
                {
                    m_WebApp.Services.ComputeStatistics();
                }

                httpResponse.ContentType = "text/html";

                return(WebAppUtils.StringToBytes(m_WebApp.Services.DefaultRequest(env)));
            }
            else
            {
                string   resourcePath  = WebApp.GetPath(resource);
                string[] resourcePaths = WebApp.GetPaths(resource);

                string type = WebAppUtils.GetContentType(resource);
                httpResponse.ContentType = type;
                //m_log.DebugFormat("[Wifi]: ContentType {0}", type);
                //m_log.DebugFormat("[XXX]: path {0}", resourcePath);
                if (type.StartsWith("image"))
                {
                    return(WebAppUtils.ReadBinaryResource(resourcePaths));
                }

                if (type.StartsWith("application"))
                {
                    string res = WebAppUtils.ReadTextResource(resourcePaths, WebApp.MissingPage, true);
                    return(WebAppUtils.StringToBytes(res));
                }
                if (type.StartsWith("text"))
                {
                    if (m_WebApp.StatisticsUpdateInterval != TimeSpan.Zero)
                    {
                        m_WebApp.Services.ComputeStatistics();
                    }

                    resourcePath = Localization.LocalizePath(env, resource);
                    Processor p   = new Processor(m_WebApp.WifiScriptFace, env);
                    string    res = p.Process(WebAppUtils.ReadTextResource(resourcePaths, WebApp.MissingPage));
                    if (res == string.Empty)
                    {
                        res = m_WebApp.Services.DefaultRequest(env);
                    }
                    return(WebAppUtils.StringToBytes(res));
                }
            }

            httpResponse.ContentType = "text/plain";
            string result = "Boo!";

            return(WebAppUtils.StringToBytes(result));
        }