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);

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

            Request request = WifiUtils.CreateRequest(resource, httpRequest);

            Diva.Wifi.Environment env = new Diva.Wifi.Environment(request);

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

                httpResponse.ContentType = "text/html";

                return(WifiUtils.StringToBytes(m_WebApp.Services.DefaultRequest(env)));
            }
            else
            {
                string resourcePath = System.IO.Path.Combine(WifiUtils.DocsPath, resource);
                string type         = WifiUtils.GetContentType(resource);
                httpResponse.ContentType = type;
                //m_log.DebugFormat("[Wifi]: ContentType {0}", type);
                if (type.StartsWith("image"))
                {
                    return(WifiUtils.ReadBinaryResource(resourcePath));
                }

                if (type.StartsWith("application"))
                {
                    string res = WifiUtils.ReadTextResource(resourcePath, true);
                    return(WifiUtils.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(WifiUtils.ReadTextResource(resourcePath));
                    if (res == string.Empty)
                    {
                        res = m_WebApp.Services.DefaultRequest(env);
                    }
                    return(WifiUtils.StringToBytes(res));
                }
            }

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

            return(WifiUtils.StringToBytes(result));
        }
Esempio n. 2
0
        public override byte[] Handle(string path, Stream requestData,
                                      IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
        {
            // It's a POST, so we need to read the data on the stream, the lines after the blank line
            StreamReader sr   = new StreamReader(requestData);
            string       body = sr.ReadToEnd();

            sr.Close();
            body = body.Trim();

            httpResponse.ContentType = "text/html";

            string resource = GetParam(path);

            //m_log.DebugFormat("[HYPERLINK POST HANDLER]: query String: {0}; resource: {1}", body, resource);

            try
            {
                // Here the data on the stream is transformed into a nice dictionary of keys & values
                Dictionary <string, object> postdata = ServerUtils.ParseQueryString(body);
                Request request           = WifiUtils.CreateRequest(resource, httpRequest);
                Diva.Wifi.Environment env = new Diva.Wifi.Environment(request);

                string result = string.Empty;
                if (resource.StartsWith("/add"))
                {
                    // The client invoked /wifi/linkregion/add
                    string address = string.Empty;
                    uint   xloc = 0, yloc = 0;
                    if (postdata.ContainsKey("address"))
                    {
                        address = postdata["address"].ToString();
                    }
                    if (postdata.ContainsKey("xloc"))
                    {
                        UInt32.TryParse(postdata["xloc"].ToString(), out xloc);
                    }
                    if (postdata.ContainsKey("yloc"))
                    {
                        UInt32.TryParse(postdata["yloc"].ToString(), out yloc);
                    }

                    result = m_WebApp.Services.HyperlinkAddRequest(env, address, xloc, yloc);
                }
                else if (resource.StartsWith("/delete"))
                {
                    // The client invoked /wifi/linkregion/delete, possibly with the UUID parameter after
                    UUID     regionID = UUID.Zero;
                    string[] pars     = SplitParams(path);
                    if ((pars.Length >= 2) && UUID.TryParse(pars[1], out regionID))
                    {
                        // Indeed the client invoked /wifi/linkregion/delete/<uuid>, and we got it already in regionID (above)
                        result = m_WebApp.Services.HyperlinkDeletePostRequest(env, regionID);
                    }
                }
                return(WifiUtils.StringToBytes(result));
            }
            catch (Exception e)
            {
                m_log.DebugFormat("[HYPERLINK POST HANDLER]: Exception {0}", e);
            }

            return(WifiUtils.FailureResult());
        }