Beispiel #1
0
        static void Main(string[] args)
        {
            var config                = HostConfig.GetHostConfig();
            var nativeModules         = GetNativeModules(config);
            var managedModules        = GetManagedModule(config);
            HttpNativeRuntime runtime = null;

            try
            {
                int  workerProcessId = Process.GetCurrentProcess().Id;
                ILog log             = LogManager.GetLogger("WorkerProcess");
                using (NamedPipeClientStream pipeStream = new NamedPipeClientStream($"{workerProcessId}.Request"))
                {
                    Byte[] bytes = new Byte[10];
                    Char[] chars = new Char[10];

                    using (NamedPipeServerStream responsePipeStream = new NamedPipeServerStream($"{workerProcessId}.Response",
                                                                                                PipeDirection.InOut, NamedPipeServerStream.MaxAllowedServerInstances, PipeTransmissionMode.Message, PipeOptions.None))
                    {
                        while (true)
                        {
                            string message = null;
                            if (!pipeStream.IsConnected)
                            {
                                pipeStream.Connect();
                                pipeStream.ReadMode = PipeTransmissionMode.Message;
                            }
                            do
                            {
                                int numBytes = pipeStream.Read(bytes, 0, bytes.Length);
                                int numChars = Encoding.UTF8.GetChars(bytes, 0, numBytes, chars, 0);
                                if (numBytes > 0)
                                {
                                    message += new String(chars, 0, numChars);
                                }
                            } while (!pipeStream.IsMessageComplete);

                            if (!string.IsNullOrEmpty(message))
                            {
                                if (runtime == null)
                                {
                                    runtime = new HttpNativeRuntime(nativeModules, managedModules);
                                    var site = config.GetRequestingSite(message);
                                    runtime.PhysicalPath = site.PhysicalPath;
                                }
                                responsePipeStream.WaitForConnection();
                                var data = runtime.ProcessRequest(message);
                                responsePipeStream.Write(data, 0, data.Length);
                                responsePipeStream.Disconnect();
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.Write(e);
                Console.Read();
            }
        }
Beispiel #2
0
        public void GetCurrentSiteTest()
        {
            var request = @"GET / HTTP/1.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: zh-Hans-CN,zh-Hans;q=0.8,en-US;q=0.5,en;q=0.3
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/18.17763
Accept-Encoding: gzip, deflate, peerdist
Host: localhost:81
Connection: Keep-Alive
X-P2P-PeerDist: Version=1.1
X-P2P-PeerDistEx: MinContentInformation=1.0, MaxContentInformation=2.0";
            var config  = HostConfig.GetHostConfig();
            var site    = config.GetRequestingSite(request);

            Assert.IsNotNull(site);
        }
Beispiel #3
0
        private void NativeRuntime_ExecuteHandler(object sender, HttpNativeContext e)
        {
            log.Info($"{this.GetType().ToString()} executing Handler");
            var request      = e.Request;
            var lines        = request.Replace("\r\n", "\r").Split('\r');
            var requestLines = lines[0].Split(' ');
            var httpMethod   = requestLines[0];
            var url          = requestLines[1].Replace("/", "\\");

            Console.WriteLine($"Requesting URL:{url}, Requesting Site PhysicalPaht: {e.Runtime.PhysicalPath}");
            var resourcePath = e.Runtime.PhysicalPath + url;

            Console.WriteLine($"Mapp request to {resourcePath}");
            if (!File.Exists(resourcePath))
            {
                var body = Encoding.UTF8.GetBytes("404 Not Found");
                e.ResponseHeader = this.GetResponse("404", "Not Found", "text/html", body);
                e.ResponseBody   = body;
            }
            else
            {
                ///I assume static file module in real IIS would skip the extension that already registed by other handler
                var extensionName = Path.GetExtension(resourcePath);
                var config        = HostConfig.GetHostConfig();
                if (config.StaticContentMapping.ContainsKey(extensionName.Trim()))
                {
                    log.Info($"Request Resource is {extensionName}, StaticFileModule takes over it");
                    var mimeType       = config.StaticContentMapping[extensionName];
                    var body           = File.ReadAllBytes(resourcePath);
                    var responseHeader = this.GetResponse("200", "ok", mimeType, body);
                    e.ResponseHeader = responseHeader;
                    e.ResponseBody   = body;
                }
                else
                {
                    var body = Encoding.UTF8.GetBytes($"500 No Handler For {extensionName} file");
                    e.ResponseHeader = this.GetResponse("500", "Internal Error", "text/html", body);
                    e.ResponseBody   = body;
                }
            }
        }