コード例 #1
0
        internal static byte[] ProcessRequestedFile(HttpRequestHeader requestHeader, Settings settings, out bool doesItApply, HttpRespondHeader resh)
        {
            doesItApply = false;
            if (requestHeader.AbsoluteFilePath.EndsWith(".py"))
            {
                // .Net's IronBython Powerfull Engine

                return(PythonRunner.Run(requestHeader, resh)); //System.IO.File.ReadAllBytes(requestHeader.AbsoluteFilePath);
            }
            else if (requestHeader.AbsoluteFilePath.EndsWith(".py3"))
            {
                // Python's C-based Engine
                return(ProcessPython3Script(requestHeader, settings));
            }
            else if (requestHeader.AbsoluteFilePath.EndsWith(".jss"))
            {
                return(ChakraCoreObject.RunScript(requestHeader.AbsoluteFilePath, requestHeader, resh));
            }
            else if (requestHeader.AbsoluteFilePath.EndsWith(".exe"))
            {
                // Native request processing app
                return(RunNativeRequestProcessorApp(requestHeader, settings));
            }
            else
            {
                // Just read bytes and pass out
                doesItApply = true;
                int count = 0;
RE_TRY:
                if (count > 10)
                {
                    return(null);
                }
                byte[] b;
                try { b = System.IO.File.ReadAllBytes(requestHeader.AbsoluteFilePath); }
                catch { count++; System.Threading.Thread.Sleep(50); goto RE_TRY; }
                return(b);
            }
        }
コード例 #2
0
        public static HttpRespondHeader GenerateRespond(HttpRequestHeader requestHeader, HttpServers.BaseServer server)
        {
            if (Rule.Corrupted.Equals(requestHeader.Repository))
            {
                throw new NotSupportedException(string.Format("[HttpRequestHeader].Repository is Corrupted!"));
            }

            HttpRespondHeader respond = new HttpRespondHeader();

            if (requestHeader.HeaderKeys.ContainsKey("Resolved-Location")) /* [DUPRECATED SCOPE] */
            {
                respond.SetState(requestHeader.HttpVersion, HttpRespondState.MOVED_PERMANENTLY);
                respond.AddHeader("Location", requestHeader.HeaderKeys["Resolved-Location"]);
            }
            else
            {
                bool   headerState = string.IsNullOrEmpty(requestHeader.AbsoluteFilePath);
                string mimeType    = headerState ? "plain/text" : MimeType.GetMimeTypeFromFile(requestHeader.AbsoluteFilePath);

                bool canICacheThisResource = false;



                if (headerState)
                {
                    respond.SetState(requestHeader.HttpVersion, HttpRespondState.NOT_FOUND);
                    respond.AddHeader("Server", CurrentServerVersion.ToString());
                }
                else
                {
                    byte[] respondBody = headerState ? null : ProcessRequestedFile(requestHeader, server.Settings, out canICacheThisResource, respond);


                    if (respondBody != null && !respondBody.Equals(HttpRequestHeader.ContentSplitter))
                    {
                        ///
                        ///  FOR REGULAR FILES AND SCRIPTS WHOME NOT USING SELF-RESPOND PROCEDURES
                        ///
                        respond.SetState(requestHeader.HttpVersion, (respondBody != null && respondBody.Length > 0) || respond.P_BodySet ? HttpRespondState.OK : HttpRespondState.NO_CONTENT);
                        respond.AddHeader("Server", ModetorServerVersion.V1_0.ToString());

                        respond.AddHeader("Connection", "close");//keep-alive
                        if ((respondBody != null && respondBody.Length > 0) || respond.P_BodySet)
                        {
                            if (canICacheThisResource)
                            {
                                if ((bool)server.Settings.Current.DebugMode)
                                {
                                    if ((bool)server.Settings.Current.EnableCachingFrontendFiles)
                                    {
                                        respond.AddHeader("Cache-Control", "public, max-age=604800, immutable");
                                    }
                                    else if (!(requestHeader.AbsoluteFilePath.EndsWith(EXT_JAVASCRIPT) || requestHeader.AbsoluteFilePath.EndsWith(EXT_CSS) || requestHeader.AbsoluteFilePath.EndsWith(EXT_HTML)))
                                    {
                                        respond.AddHeader("Cache-Control", "public, max-age=604800, immutable");
                                    }
                                }
                                else
                                {
                                    respond.AddHeader("Cache-Control", "public, max-age=604800, immutable");
                                }
                            }


                            respond.AddHeader("Content-Type", mimeType);//+ "; charset=UTF-8"

                            respond.AddHeader("Content-Length", respond.P_BodySet ? respond.RespondBuffer[2].Length.ToString() : respondBody.Length.ToString());

                            respond.SetBody(respondBody);
                        }
                    }
                    else
                    {
                        ///
                        /// THIS FOR SCRIPTS WITH SELF-RESPOND PROCEDURES
                        ///
                    }
                }
            }



            return(respond);
        }
コード例 #3
0
        public static byte[] RunScript(string file, HttpRequestHeader req, HttpRespondHeader res)
        {
            using (ChakraContext context = Runtime.CreateContext(true)) {
                //HttpRequestHeader
                if (!registered)
                {
                    context.ServiceNode.GetService <IJSValueConverterService>().RegisterProxyConverter <HttpRequestHeader>( //register the object converter
                        (binding, instance, serviceNode) => {
                        binding.SetFunction("getServer", new Func <HttpServers.BaseServer>(() => instance.Server));
                        binding.SetFunction("getHeaderKeys", new Func <Dictionary <string, string> >(() => instance.HeaderKeys));
                        binding.SetFunction("getParameters", new Func <System.Collections.Specialized.NameValueCollection>(() => instance.Parameters));
                        binding.SetFunction("getParametersJSON", new Func <string>(() => instance.Parameters.Serialize()));
                        binding.SetFunction("getAbsoluteFilePath", new Func <string>(() => instance.AbsoluteFilePath));
                    });
                    // HttpRespondHeader
                    context.ServiceNode.GetService <IJSValueConverterService>().RegisterProxyConverter <HttpRespondHeader>( //register the object converter
                        (binding, instance, serviceNode) =>
                    {
                        binding.SetMethod <string, string>("setState", (http, state) => instance.SetState(http.Equals("HTTP/1.0") ? HttpVersion.HTTP1_0 : http.Equals("HTTP/1.1") ? HttpVersion.HTTP2_0 : HttpVersion.UNKNOWN, state));
                        binding.SetMethod <string, string>("addHeader", (key, value) => instance.AddHeader(key, value));
                        binding.SetMethod <string>("setBody", (body) => instance.SetBody(body));
                        binding.SetMethod <System.Net.Sockets.TcpClient>("send", c => {
                            byte[] b = instance.Build();
                            c.GetStream().Write(b, 0, b.Length);
                            c.GetStream().Flush();
                        });
                        binding.SetMethod("markAsResponded", instance.Responded);
                    });
                    // TcpClient stuff
                    context.ServiceNode.GetService <IJSValueConverterService>().RegisterProxyConverter <System.Net.Sockets.TcpClient>( //register the object converter
                        (binding, instance, serviceNode) =>
                    {
                        binding.SetMethod <string>("sendText", (s) => {
                            byte[] b = Encoding.UTF8.GetBytes(s);
                            instance.GetStream().Write(b, 0, b.Length);
                            instance.GetStream().Flush();
                        });
                        binding.SetMethod <HttpRespondHeader>("send", (s) => {
                            byte[] b = s.Build();
                            instance.GetStream().Write(b, 0, b.Length);
                            instance.GetStream().Flush();
                        });
                        binding.SetFunction <string>("getIP", () => ((System.Net.IPEndPoint)instance.Client.RemoteEndPoint).Address.ToString());
                        binding.SetFunction <int>("getPort", () => ((System.Net.IPEndPoint)instance.Client.RemoteEndPoint).Port);
                        binding.SetMethod("close", instance.Close);
                        binding.SetFunction <string>("readString", () => {
                            byte[] b = new byte[instance.Available];
                            instance.GetStream().Read(b, 0, b.Length);
                            return(Encoding.UTF8.GetString(b));
                        });
                        binding.SetMethod <string, bool>("sendFile", (file, isBigFile) => { instance.Client.SendFile(file, null, null, isBigFile ? System.Net.Sockets.TransmitFileOptions.UseSystemThread : System.Net.Sockets.TransmitFileOptions.UseDefaultWorkerThread); });
                    });

                    registered = true;
                }


                context.GlobalObject.WriteProperty("RequestResult", "");
                context.GlobalObject.WriteProperty("RequestHandler", req);
                context.GlobalObject.WriteProperty("RespondHandler", res);
                context.GlobalObject.WriteProperty("Client", req.Client);
                context.GlobalObject.WriteProperty("AutoFlush", true);
                context.GlobalObject.Binding.SetMethod <string>("log", (s) => Console.WriteLine(s));
                context.GlobalObject.Binding.SetMethod <string>("send", (s) => {
                    req.Client.GetStream().Write(Encoding.UTF32.GetBytes(s), 0, s.Length);
                    if (context.GlobalObject.ReadProperty <bool>("AutoFlush"))
                    {
                        req.Client.GetStream().Flush();
                    }
                });
                context.GlobalObject.Binding.SetMethod("flush", () => req.Client.GetStream().Flush());
                context.GlobalObject.Binding.SetMethod("closeStream", () => req.Client.GetStream().Dispose());
                context.GlobalObject.Binding.SetMethod("closeConnection", () => req.Client.Close());

                try
                {
                    context.RunScript(System.IO.File.ReadAllText(file));
                }
                catch (Exception exp)
                {
                    ErrorLogger.WithTrace(req.Server.Settings, string.Format("[Warning][Backend error => RunScript()] : exception-message : {0}.\nstacktrace : {1}\n", exp.Message, exp.StackTrace), typeof(ChakraCoreObject));
                }

                if (res.DidRespond())
                {
                    Runtime.CollectGarbage();
                    return(HttpRequestHeader.ContentSplitter);
                }

                else
                {
                    byte[] b = Encoding.UTF8.GetBytes(context.GlobalObject.ReadProperty <string>("RequestResult"));
                    Runtime.CollectGarbage();
                    return(b);
                }
            }


            //context.Dispose();
        }