Exemplo n.º 1
0
        /// <summary>
        /// Request handler function. Overriden from RequestHandler class
        /// </summary>
        /// <param name="context">Request context</param>
        public override void HandleReqest(HttpListenerContext context)
        {
            _content.Append(TemplateText);

            if (context.Request.HttpMethod == "GET")
            {
                if (GetHandler != null)
                {
                    GetHandler(this, new HTTPEventArgs(EmbeddedHttpServer.GetFileName(context.Request.RawUrl), context.Request.QueryString));
                }
            }

            if (TemplateTags != null)
            {
                foreach (var keyvaluepair in TemplateTags)
                {
                    var toreplace = "{{" + keyvaluepair.Key + "}}";
                    _content.Replace(toreplace, keyvaluepair.Value.ToString());
                }
            }

            var output = Encoding.UTF8.GetBytes(_content.ToString());

            context.Response.ContentType     = ResponseMime;
            context.Response.ContentLength64 = output.Length;
            context.Response.OutputStream.Write(output, 0, output.Length);
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            TemplateHandler templatetest = new TemplateHandler();
            templatetest.RegisterHooks("templatetest");
            templatetest.ResponseMime = "text/plain";
            templatetest.TemplateText = "Template test. Query parameters: {{query}}";
            templatetest.GetHandler += templatetest_GetHandler;

            EmbeddedHttpServer server = new EmbeddedHttpServer(8080);
            server.Path = Environment.CurrentDirectory;
            server.Handlers.Add(templatetest);
            server.LogDir = Path.Combine(Environment.CurrentDirectory, "Log");
            server.AppPackages.Add(Path.Combine(Environment.CurrentDirectory, "webdev.zip"));
            server.Start();
            Console.WriteLine("Server running. Press ESC to quit");
            ConsoleKeyInfo key;
            do
            {
                key = Console.ReadKey();
            }
            while (key.Key != ConsoleKey.Escape);
            server.Stop();
        }