Esempio n. 1
0
        public void SendResponse(HttpListenerContext context)
        {
            if (Engine.Instance.Manifest == null)
            {
                return;                 // Not ready... do better
            }
            // string physicalPath = GetPath() + request.RawUrl;
            string bodyResponse = "";             // If valorized, always a dynamic response
            Dictionary <string, string> requestHeaders = new Dictionary <string, string>();

            foreach (var key in context.Request.Headers.AllKeys)
            {
                requestHeaders[key.ToLowerInvariant()] = context.Request.Headers[key];
            }
            string requestHttpMethod = context.Request.HttpMethod.ToLowerInvariant().Trim();

            context.Response.Headers["Server"] = Constants.Name + " " + Constants.VersionShow;
            context.Response.Headers["Access-Control-Allow-Origin"] = ListenUrl;
            context.Response.Headers["Vary"] = "Origin";

            foreach (KeyValuePair <string, object> jsonHeader in Engine.Instance.Manifest["webserver"]["headers"]["common"].Json.GetDictionary())
            {
                string k = jsonHeader.Key;
                string v = (jsonHeader.Value as string);
                context.Response.Headers[k] = v;
            }

            string origin = context.Request.Headers["Origin"];

            if ((requestHeaders.ContainsKey("origin")) && (requestHeaders["origin"].StartsWith(ListenUrl) == false))
            {
                List <string> hostsAllowed = new List <string>();               // Option?
                hostsAllowed.Add("127.0.0.1");
                hostsAllowed.Add("localhost");
                Uri uriOrigin = new Uri(requestHeaders["origin"]);
                if (hostsAllowed.Contains(uriOrigin.Host) == false)
                {
                    context.Response.StatusCode = (int)HttpStatusCode.Forbidden;
                    return;
                }
            }

            if (requestHttpMethod == "options")
            {
                Engine.Instance.Logs.LogVerbose(origin);
                context.Response.StatusCode = (int)HttpStatusCode.NoContent;
            }

            if (context.Request.Url.AbsolutePath == "/api/command/")
            {
                if (requestHttpMethod == "post")
                {
                    // Pull mode
                    var  data = new StreamReader(context.Request.InputStream).ReadToEnd();
                    Json ret  = Receive(data);
                    if (ret != null)
                    {
                        bodyResponse = ret.ToJson();
                    }
                    else
                    {
                        bodyResponse = "null";
                    }
                }
                else
                {
                    context.Response.StatusCode = (int)HttpStatusCode.NoContent;
                }
            }
            else if (context.Request.Url.AbsolutePath == "/api/pull/")
            {
                if (requestHttpMethod == "post")
                {
                    lock (m_client.Pendings)
                    {
                        if (m_client.Pendings.Count == 0)
                        {
                            bodyResponse = "null";
                        }
                        else
                        {
                            Json data = m_client.Pendings[0];
                            m_client.Pendings.RemoveAt(0);
                            bodyResponse = data.ToJson();
                        }
                    }
                }
                else
                {
                    context.Response.StatusCode = (int)HttpStatusCode.NoContent;
                }
            }
            else
            {
                string urlPath = context.Request.Url.LocalPath;
                if (urlPath == "/")
                {
                    urlPath = "/index.html";
                }
                string localPath = GetPath() + urlPath;
                if (Platform.Instance.FileExists(localPath))
                {
                    if (context.Request.HttpMethod == "GET")
                    {
                        WriteFile(context, localPath, false);
                    }
                    else
                    {
                        throw new Exception("Unexpected");
                    }
                }
                else
                {
                    context.Response.StatusCode = (int)HttpStatusCode.NotFound;
                }
            }

            if (bodyResponse != "")            // Always dynamic
            {
                context.Response.Headers["Cache-Control"] = "no-store, no-cache, must-revalidate, max-age=0";
                context.Response.Headers["Pragma"]        = "no-cache";

                byte[] buf = Encoding.UTF8.GetBytes(bodyResponse);
                context.Response.ContentLength64 = buf.Length;
                context.Response.OutputStream.Write(buf, 0, buf.Length);
            }
        }
Esempio n. 2
0
 public void SetJson(string name, Json val)
 {
     Set(name, val.ToJson());
 }