public MappedFolderController(string name, string rootFolder, HttpServer httpServer) : base(name, httpServer)
        {
            Handle(HttpMethod.Get, string.Empty).WithAnySubUrl().Using(c =>
            {
                string relativeUrl = c.Request.Uri.Trim('/');
                int index = relativeUrl.IndexOf(name, StringComparison.OrdinalIgnoreCase);
                if (index == -1)
                {
                    return;
                }

                relativeUrl = relativeUrl.Substring(index + name.Length).Replace("/", "\\");
                relativeUrl = Uri.UnescapeDataString(relativeUrl);
                string filename = rootFolder + relativeUrl;

                if (File.Exists(filename))
                {
                    byte[] fileContent = File.ReadAllBytes(filename);
                    string mimeType = _mimeTypeProvider.GetMimeTypeFromFile(filename);

                    c.Response.Body = new BinaryBody(fileContent).WithMimeType(mimeType);
                }
                else
                {
                    c.Response.StatusCode = HttpStatusCode.NotFound;
                }
            });
        }
        public HttpRequestController(string name, HttpServer server)
        {
            if (name == null) throw new ArgumentNullException(nameof(name));
            if (server == null) throw new ArgumentNullException(nameof(server));

            _baseUri = name;
            server.RequestReceived += ExecuteActions;
        }
        private void InitializeHttpApi()
        {
            _httpServer = new HttpServer();
            var httpRequestDispatcher = new HttpRequestDispatcher(_httpServer);
            HttpApiController = httpRequestDispatcher.GetController("api");

            var appPath = Path.Combine(ApplicationData.Current.LocalFolder.Path, "app");
            httpRequestDispatcher.MapFolder("app", appPath);
        }
예제 #4
0
 public void InitializeHttpApi()
 {
     _httpServer = new HttpServer();
     HttpRequestDispatcher httpRequestDispatcher = new HttpRequestDispatcher(_httpServer);
     _apiController = httpRequestDispatcher.GetController("api");
     _apiController.Handle(HttpMethod.Get, "read").Using(HandleApiRead);
     _apiController.Handle(HttpMethod.Post, "write").Using(HandleApiWrite);
     _httpServer.StartAsync(80).Wait();
 }
        public HttpRequestDispatcher(HttpServer server)
        {
            if (server == null) throw new ArgumentNullException(nameof(server));

            _server = server;
        }