コード例 #1
0
ファイル: Program.cs プロジェクト: fastlater/PiServerLite
        static int Main()
        {
            var context = new HttpReceiverContext {
                ListenerPath = "/piServer",
            };

            context.ContentDirectories.Add(new ContentDirectory {
                DirectoryPath = ".\\Content",
                UrlPath       = "/Content/",
            });

            context.Views.AddFolderFromExternal(".\\Views");

            if (!StartReceiver(context))
            {
                Console.WriteLine();
                Console.WriteLine("Press any key to exit...");
                Console.ReadKey(true);
                return(1);
            }

            Console.WriteLine();
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("Http Server is running. Press any key to stop...");
            Console.WriteLine();
            Console.ReadKey(true);
            Console.ResetColor();
            Console.WriteLine();

            StopReceiver();

            Console.ResetColor();
            return(0);
        }
コード例 #2
0
        private void StartHttpServer()
        {
            var enableSecurity = ServerConfiguration.Value.Security?.Enabled ?? false;
            var http           = ServerConfiguration.Value.Http;

            var contentDir = new ContentDirectory {
                DirectoryPath = Configuration.HttpContentDirectory,
                UrlPath       = "/Content/",
            };

            var sharedContentDir = new ContentDirectory {
                DirectoryPath = Configuration.HttpSharedContentDirectory,
                UrlPath       = "/SharedContent/",
            };

            var context = new HttpReceiverContext {
                ListenerPath       = http.Path,
                ContentDirectories =
                {
                    contentDir,
                    sharedContentDir,
                },
            };

            if (enableSecurity)
            {
                var ldapAuth = new LdapAuthorization();

                context.SecurityMgr = new ServerHttpSecurity {
                    Authorization = ldapAuth,
                };
            }

            context.Views.AddFolderFromExternal(Configuration.HttpViewDirectory);

            var httpPrefix = $"http://{http.Host}:{http.Port}/";

            if (!string.IsNullOrEmpty(http.Path))
            {
                httpPrefix = NetPath.Combine(httpPrefix, http.Path);
            }

            if (!httpPrefix.EndsWith("/"))
            {
                httpPrefix += "/";
            }

            receiver = new HttpReceiver(context);
            receiver.Routes.Scan(Assembly.GetExecutingAssembly());
            receiver.AddPrefix(httpPrefix);

            try {
                receiver.Start();

                Log.Debug($"HTTP Server listening at {httpPrefix}");
            }
            catch (Exception error) {
                Log.Error("Failed to start HTTP Receiver!", error);
            }
        }
コード例 #3
0
        /// <summary>
        /// Redirect the response to a relative path.
        /// </summary>
        /// <returns>HTTP [302] Redirect.</returns>
        public static HttpHandlerResult Redirect(HttpReceiverContext context, string path, object queryArgs = null)
        {
            var urlUtility = new UrlUtility(context);
            var url        = urlUtility.GetRelative(path, queryArgs);

            return(new HttpHandlerResult {
                StatusCode = (int)HttpStatusCode.Redirect,
                redirectUrl = url,
            });
        }
コード例 #4
0
        public static HttpHandlerResult File(HttpReceiverContext context, string filename)
        {
            var ext = Path.GetExtension(filename);

            return(new HttpHandlerResult {
                StatusCode = (int)HttpStatusCode.OK,
                StatusDescription = "OK.",
                ContentType = context.MimeTypes.Get(ext),
                ContentLength = new FileInfo(filename).Length,
                streamContentFunc = () => System.IO.File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.Read),
            });
        }
コード例 #5
0
ファイル: PhotonAgent.cs プロジェクト: lulzzz/Photon
        private void StartHttpServer()
        {
            var contentDir = new ContentDirectory {
                DirectoryPath = Configuration.HttpContentDirectory,
                UrlPath       = "/Content/",
            };

            var sharedContentDir = new ContentDirectory {
                DirectoryPath = Configuration.HttpSharedContentDirectory,
                UrlPath       = "/SharedContent/",
            };

            var context = new HttpReceiverContext {
                SecurityMgr        = new AgentHttpSecurity(),
                ListenerPath       = Definition.Http.Path,
                ContentDirectories =
                {
                    contentDir,
                    sharedContentDir,
                },
            };

            context.Views.AddFolderFromExternal(Configuration.HttpViewDirectory);

            var httpPrefix = $"http://{Definition.Http.Host}:{Definition.Http.Port}/";

            if (!string.IsNullOrEmpty(Definition.Http.Path))
            {
                httpPrefix = NetPath.Combine(httpPrefix, Definition.Http.Path);
            }

            if (!httpPrefix.EndsWith("/"))
            {
                httpPrefix += "/";
            }

            receiver = new HttpReceiver(context);
            receiver.Routes.Scan(Assembly.GetExecutingAssembly());
            receiver.AddPrefix(httpPrefix);

            try {
                receiver.Start();

                Log.Info($"HTTP Server listening at {httpPrefix}");
            }
            catch (Exception error) {
                Log.Error("Failed to start HTTP Receiver!", error);
            }
        }
コード例 #6
0
        public HttpServer(PhotonAgent agent)
        {
            this.agent = agent;

            Authorization = new HybridAuthorization();

            Security = new HttpSecurityManager {
                Authorization = Authorization,
                CookieName    = "PHOTON.AGENT.AUTH",
            };

            Context = new HttpReceiverContext {
                SecurityMgr = Security,
            };
        }
コード例 #7
0
ファイル: HttpServer.cs プロジェクト: wlclass/Photon
        public HttpServer(PhotonServer server)
        {
            this.server = server;

            Authorization = new HybridAuthorization();

            Security = new HttpSecurityManager {
                Authorization = Authorization,
                CookieName    = "PHOTON.SERVER.AUTH",
            };

            Context = new HttpReceiverContext {
                SecurityMgr = Security,
            };
        }
コード例 #8
0
        /// <summary>
        /// Returns a named view from the <see cref="ViewCollection"/>.
        /// </summary>
        /// <param name="name">The name of the view.</param>
        /// <param name="param">Optional view-model object.</param>
        public static HttpHandlerResult View(HttpReceiverContext context, string name, object param = null)
        {
            if (!context.Views.TryFind(name, out var content))
            {
                throw new ApplicationException($"View '{name}' was not found!");
            }

            var engine = new HtmlEngine(context.Views)
            {
                UrlRoot = context.ListenerPath,
            };

            content = engine.Process(content, param);

            return(new HttpHandlerResult {
                StatusCode = (int)HttpStatusCode.OK,
                StatusDescription = "OK.",
            }.SetText(content));
        }
コード例 #9
0
ファイル: Program.cs プロジェクト: fastlater/PiServerLite
        private static bool StartReceiver(HttpReceiverContext context)
        {
            Console.ResetColor();
            Console.WriteLine("Starting Http Server...");
            Console.WriteLine("Listening at http://*/piServer");

            receiver = new HttpReceiver(context);
            receiver.AddPrefix("http://*:80/piServer/");

            try {
                receiver.Routes.Scan(Assembly.GetExecutingAssembly());

                receiver.Start();
                Console.ForegroundColor = ConsoleColor.DarkGreen;
                Console.WriteLine("  Http Server started successfully.");
                return(true);
            }
            catch (Exception error) {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine($"  Failed to start Http Server! {error}");
                return(false);
            }
        }
コード例 #10
0
ファイル: AgentHttpSecurity.cs プロジェクト: lulzzz/Photon
        public HttpHandlerResult OnUnauthorized(HttpListenerContext httpContext, HttpReceiverContext context)
        {
            var returnUrl = httpContext.Request.RawUrl;

            return(HttpHandlerResult.Redirect(context, "/login", new { returnUrl }));
        }
コード例 #11
0
        internal IHttpHandler GetHandler(HttpRouteDescription routeDescription, HttpListenerContext httpContext, HttpReceiverContext context)
        {
            if (!(Activator.CreateInstance(routeDescription.ClassType) is IHttpHandler handler))
            {
                throw new ApplicationException($"Unable to construct HttpHandler implementation '{routeDescription.ClassType.Name}'!");
            }

            handler.HttpContext = httpContext;
            handler.Context     = context;
            handler.OnRequestReceived();
            return(handler);
        }
コード例 #12
0
 public UrlUtility(HttpReceiverContext context)
 {
     this.RootPath = context.ListenerPath;
 }