public WorkflowState GetContent(WorkflowContinuation <HttpListenerContext> workflowContinuation, HttpListenerContext context)
        {
            // Get the request.
            HttpListenerRequest  request  = context.Request;
            HttpListenerResponse response = context.Response;

            // Get the path, everything up to the first ? and excluding the leading "/"
            string path = context.Path();
            string ext  = context.Extension();

            // Default to index.html if only the URL is provided with no additional page information.
            if (String.IsNullOrEmpty(path))
            {
                path = "index.html";
                ext  = "html";
            }

            if (String.IsNullOrEmpty(ext))
            {
                path = path + ".html";
            }

            path = websitePath + "\\" + path;
            FileExtensionHandler extHandler;

            if (extensionLoaderMap.TryGetValue(ext, out extHandler))
            {
                byte[] data = extHandler.Loader(context, path, ext);
                response.ContentEncoding         = Encoding.UTF8;
                context.Response.ContentType     = extHandler.ContentType;
                context.Response.ContentLength64 = data.Length;
                context.Response.OutputStream.Write(data, 0, data.Length);
                response.StatusCode = 200;                                      // OK
                response.OutputStream.Close();
            }

            return(WorkflowState.Done);
        }
 public UriExtension Extension()
 {
     return(context.Extension());
 }
Exemplo n.º 3
0
        public bool ProcessFileRequest(HttpListenerContext context)
        {
            bool   handled = false;
            string path    = context.Path().Value.Replace('/', '\\').LeftOfRightmostOf('.');            // without extension
            string ext     = context.Extension().Value;

            if (String.IsNullOrEmpty(path))
            {
                path = "index";
            }

            if (String.IsNullOrEmpty(ext))
            {
                ext = "html";
            }

            path = path + "." + ext;
            path = Path.Combine(websitePath, path);

            if (File.Exists(path))
            {
                switch (ext)
                {
                case "html":
                case "spa":
                    ServiceManager.Get <ISemanticProcessor>().ProcessInstance <WebServerMembrane, HtmlResponse>(r =>
                    {
                        r.Context = context;
                        r.Html    = ReadTextFile(path);
                    });
                    break;

                case "woff":
                case "woff2":
                case "ttf":
                    ServiceManager.Get <ISemanticProcessor>().ProcessInstance <WebServerMembrane, FontResponse>(r =>
                    {
                        r.Context     = context;
                        r.ContentType = "font/" + ext;
                        r.BinaryData  = ReadBinaryFile(path);
                    });
                    break;

                case "js":
                    ServiceManager.Get <ISemanticProcessor>().ProcessInstance <WebServerMembrane, JavascriptResponse>(r =>
                    {
                        r.Context = context;
                        r.Script  = ReadTextFile(path);
                    });
                    break;

                case "css":
                    ServiceManager.Get <ISemanticProcessor>().ProcessInstance <WebServerMembrane, CssResponse>(r =>
                    {
                        r.Context = context;
                        r.Script  = ReadTextFile(path);
                    });
                    break;

                case "jpg":
                case "ico":
                case "png":
                case "bmp":
                case "gif":
                    ServiceManager.Get <ISemanticProcessor>().ProcessInstance <WebServerMembrane, ImageResponse>(r =>
                    {
                        r.Context     = context;
                        r.ContentType = "image/" + ext;
                        r.BinaryData  = ReadBinaryFile(path);
                    });
                    break;

                default:
                    ServiceManager.Get <ISemanticProcessor>().ProcessInstance <WebServerMembrane, StringResponse>(r =>
                    {
                        r.Context    = context;
                        r.Message    = ReadTextFile(path);
                        r.StatusCode = 200;
                    });
                    break;
                }

                handled = true;
            }

            return(handled);
        }
Exemplo n.º 4
0
 /// <inheritdoc />
 public string Extension()
 {
     return(context.Extension());
 }