Пример #1
0
        static void Main(string[] args)
        {
            // Template generators are used to render templates 
            // (convert code + html to pure html).
            TemplateManager mgr = new TemplateManager();
            mgr.Add("haml", new HamlGenerator());

            // The httpserver is quite dumb and will only serve http, nothing else.
            HttpServer server  = new HttpServer();

            // a controller mode implements a MVC pattern
            // You'll add all controllers to the same module.
            ControllerModule mod = new ControllerModule();
            mod.Add(new UserController(mgr));
            server.Add(mod);

            // file module will be handling files
            FileModule fh = new FileModule("/", Environment.CurrentDirectory);
            fh.AddDefaultMimeTypes();
            server.Add(fh);

            // Let's start pure HTTP, we can also start a HTTPS listener.
            server.Start(IPAddress.Any, 8081);

            Console.ReadLine();
        }
Пример #2
0
        private void Form1_Load(object sender, EventArgs e)
        {
            client = new WebClient();
            client.Encoding = Encoding.UTF8;

            var info = new OBBInfo();
            info.ip = IPAddress.Loopback.ToString();
            info.notifyPort = Settings.Default.notifyPort;
            info.servicePort = Settings.Default.servicePort;
            info.serviceRoot = Settings.Default.serviceRoot;
             
            if (String.IsNullOrWhiteSpace(info.serviceRoot))
                info.serviceRoot = Environment.CurrentDirectory;

            OBBContext.Current.Info = info;
            OBBContext.Current.Mode = Settings.Default.mode;

            server = new HttpServer.HttpServer();
            var fileModule = new FileModule("/", OBBContext.Current.Info.serviceRoot);
            var myModule = new MyModule();
            fileModule.AddDefaultMimeTypes();
            server.Add(myModule);
            server.Add(fileModule);
            server.Start(IPAddress.Any, OBBContext.Current.Info.servicePort);

            if (OBBContext.Current.IsMaster)
            {
                OBBContext.Current.MasterInfo = OBBContext.Current.Info;

                notifyIcon1.Icon = Resources.master;
                var port = IPAddress.HostToNetworkOrder(OBBContext.Current.Info.servicePort);
                byte[] portData = BitConverter.GetBytes(port);
                notify = new UdpNotify(OBBContext.Current.Info.notifyPort, portData);
            }
            else
            {
                OBBContext.Current.LoadGameConfig();

                notifyIcon1.Icon = Resources.slave;
                notify = new UdpNotify(OBBContext.Current.Info.notifyPort);
                notify.OnData += Notify_OnData;
            }

            this.Text = OBBContext.Current.Mode.ToString();
            notifyIcon1.Text = OBBContext.Current.Mode.ToString();
            refreshTimer.Start();
            button1.Enabled = OBBContext.Current.IsMaster;
        }
Пример #3
0
 private static void AddMimeTypes(FileModule fm)
 {
     fm.AddDefaultMimeTypes();
     fm.MimeTypes["htc"] = "text/x-component";
     fm.MimeTypes["json"] = "application/json";
     fm.MimeTypes["map"] = "application/json";
     fm.MimeTypes["htm"] = "text/html; charset=utf-8";
     fm.MimeTypes["html"] = "text/html; charset=utf-8";
     fm.MimeTypes["hbs"] = "application/x-handlebars-template";
     fm.MimeTypes["woff"] = "application/font-woff";
 }
Пример #4
0
        private static HttpServer.HttpServer CreateServer(IDictionary<string, string> options)
        {
            HttpServer.HttpServer server = new HttpServer.HttpServer();

            server.Add(new AuthenticationHandler());

            server.Add(new ControlHandler());

            string webroot = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
            string install_webroot = System.IO.Path.Combine(Library.AutoUpdater.UpdaterManager.InstalledBaseDir, "webroot");

#if DEBUG
            // Easy test for extensions while debugging
            install_webroot = Library.AutoUpdater.UpdaterManager.InstalledBaseDir;

            if (!System.IO.Directory.Exists(System.IO.Path.Combine(webroot, "webroot")))
            {
                //For debug we go "../../../.." to get out of "GUI/Duplicati.GUI.TrayIcon/bin/debug"
                string tmpwebroot = System.IO.Path.GetFullPath(System.IO.Path.Combine(webroot, "..", "..", "..", ".."));
                tmpwebroot = System.IO.Path.Combine(tmpwebroot, "Server");
                if (System.IO.Directory.Exists(System.IO.Path.Combine(tmpwebroot, "webroot")))
                    webroot = tmpwebroot;
                else
                {
                    //If we are running the server standalone, we only need to exit "bin/Debug"
                    tmpwebroot = System.IO.Path.GetFullPath(System.IO.Path.Combine(webroot, "..", ".."));
                    if (System.IO.Directory.Exists(System.IO.Path.Combine(tmpwebroot, "webroot")))
                        webroot = tmpwebroot;
                }
            }
#endif

            webroot = System.IO.Path.Combine(webroot, "webroot");

            if (options.ContainsKey(OPTION_WEBROOT))
            {
                string userroot = options[OPTION_WEBROOT];
#if DEBUG
                //In debug mode we do not care where the path points
#else
                //In release mode we check that the user supplied path is located
                // in the same folders as the running application, to avoid users
                // that inadvertently expose top level folders
                if (!string.IsNullOrWhiteSpace(userroot)
                    &&
                    (
                        userroot.StartsWith(Library.Utility.Utility.AppendDirSeparator(System.Reflection.Assembly.GetExecutingAssembly().Location), Library.Utility.Utility.ClientFilenameStringComparision)
                        ||
                        userroot.StartsWith(Library.Utility.Utility.AppendDirSeparator(Program.StartupPath), Library.Utility.Utility.ClientFilenameStringComparision)
                    )
                )
#endif
                {
                    webroot = userroot;
                    install_webroot = webroot;
                }
            }

            if (install_webroot != webroot && System.IO.Directory.Exists(System.IO.Path.Combine(install_webroot, "customized")))
            {
                var customized_files = new FileModule("/customized/", System.IO.Path.Combine(install_webroot, "customized"));
                customized_files.AddDefaultMimeTypes();
                customized_files.MimeTypes.Add("htc", "text/x-component");
                customized_files.MimeTypes.Add("json", "application/json");
                customized_files.MimeTypes.Add("map", "application/json");
                customized_files.MimeTypes["htm"] = "text/html; charset=utf-8";
                customized_files.MimeTypes["html"] = "text/html; charset=utf-8";
                server.Add(customized_files);
            }

            var fh = new FileModule("/", webroot);
            fh.AddDefaultMimeTypes();
            fh.MimeTypes.Add("htc", "text/x-component");
            fh.MimeTypes.Add("json", "application/json");
            fh.MimeTypes.Add("map", "application/json");
            fh.MimeTypes["htm"] = "text/html; charset=utf-8";
            fh.MimeTypes["html"] = "text/html; charset=utf-8";
            server.Add(fh);
            server.Add(new IndexHtmlHandler(webroot));
#if DEBUG
            //For debugging, it is nice to know when we get a 404
            server.Add(new DebugReportHandler());
#endif
            return server;
        }