Esempio n. 1
0
        public void GetMimeType_WhenFileHasContentTypeAndMimeTypeProviderHasnt()
        {
            var expectedContentType = "application/test";
            var mimeTypeProvider    = new MimeTypeProvider(new Dictionary <string, string>());
            var mimeType            = mimeTypeProvider.GetMimeType(new MockFile(string.Empty, expectedContentType, ".test"));

            Assert.AreEqual(expectedContentType, mimeType);
        }
Esempio n. 2
0
        public void GetMimeType_WhenBothFileAndMimeTypeProviderHaveGotContentType()
        {
            var expectedContentType = "application/test";
            var mimeTypeProvider    = new MimeTypeProvider(new Dictionary <string, string> {
                { ".test", expectedContentType }
            });
            var mimeType = mimeTypeProvider.GetMimeType(new MockFile(string.Empty, "application/testing", ".test"));

            Assert.AreEqual(expectedContentType, mimeType);
        }
Esempio n. 3
0
        public void GetMimeType_WhenFileDoesNotHaveContentTypeAndExtensionDoesNotHaveSameCasing()
        {
            var expectedContentType = "application/test";
            var mimeTypeProvider    = new MimeTypeProvider(new Dictionary <string, string> {
                { ".test", expectedContentType }
            });
            var mimeType = mimeTypeProvider.GetMimeType(new MockFile(string.Empty, null, ".tEsT"));

            Assert.AreEqual(expectedContentType, mimeType);
        }
        public static IApplicationBuilder UseJsonRpcApi(this IApplicationBuilder app, JsonRpcInfo jsonRpcInfo)
        {
            app.Use(async(context, next) =>
            {
                var referer     = SanitizeReferer(context.Request.GetTypedHeaders().Referer?.AbsolutePath ?? "");
                var requestPath = referer + context.Request.Path;
                var file        = JsonRpcFileReader.GetFile(requestPath, jsonRpcInfo);
                if (!file.Exist)
                {
                    await next.Invoke();
                    return;
                }
                context.Response.ContentType = MimeTypeProvider.Get(file.Extension);
                context.Response.StatusCode  = 200;
                await context.Response.Body.WriteAsync(file.Buffer, 0, file.Buffer.Length);
            });

            return(app);
        }
Esempio n. 5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="FilesConfig"/> class.
        /// </summary>
        /// <param name="dataStore">The data store.</param>
        /// <exception cref="System.ArgumentNullException">dataStore</exception>
        public FilesConfig(IReadOnlyDataStore dataStore)
        {
            if (dataStore == null)
            {
                throw new ArgumentNullException("dataStore");
            }

            DataStore      = dataStore;
            _defaultLog    = new NullLog();
            _defaultDirGen = new BootstrapDirectoryListingGenerator();
            _defaultMime   = new MimeTypeProvider();
            _defaultETag   = new ModifiedDateETagGenerator();

            DefaultDocuments = new List <string>
            {
                "index.html",
                "index.htm",
            };
        }
Esempio n. 6
0
        private static void OnAppRequestReceived(HttpContext context, string rootDirectory)
        {
            string filename;

            if (!TryGetFilename(context, rootDirectory, out filename))
            {
                context.Response.StatusCode = HttpStatusCode.BadRequest;
                return;
            }

            if (File.Exists(filename))
            {
                context.Response.Body     = File.ReadAllBytes(filename);
                context.Response.MimeType = MimeTypeProvider.GetMimeTypeFromFilename(filename);
            }
            else
            {
                context.Response.StatusCode = HttpStatusCode.NotFound;
            }
        }
Esempio n. 7
0
        public static void UseJsonRpcApi(this HttpServer server, JsonRpcInfo jsonRpcInfo)
        {
            server.OnGet += (s, e) =>
            {
                var referer     = e.Request.UrlReferrer?.AbsolutePath ?? "";
                var requestPath = referer + e.Request.Url.AbsolutePath;
                if (requestPath.StartsWith("//"))
                {
                    requestPath = requestPath.Substring(1);
                }
                var file = JsonRpcFileReader.GetFile(requestPath, jsonRpcInfo);
                if (!file.Exist)
                {
                    return;
                }
                e.Response.ContentType     = MimeTypeProvider.Get(file.Extension);
                e.Response.ContentEncoding = Encoding.UTF8;
                // allow cors
                e.Response.Headers.Add("Access-Control-Allow-Origin", "*");
                e.Response.StatusCode = 200;

                e.Response.WriteContent(file.Buffer);
            };
        }
Esempio n. 8
0
 /// <summary>
 /// Initializes a new instance of the <see cref="FileHandler"/> class.
 /// </summary>
 /// <remarks>Initializes using a <see cref="DiskFileService"/> with current directory as root.</remarks>
 public FileHandler()
 {
     _fileService      = new DiskFileService("/", Environment.CurrentDirectory);
     _mimeTypeProvider = new MimeTypeProvider();
 }
Esempio n. 9
0
 /// <summary>
 /// Initializes a new instance of the <see cref="FileHandler"/> class.
 /// </summary>
 /// <param name="fileService">The file service.</param>
 public FileHandler(IFileService fileService, MimeTypeProvider mimeTypeProvider)
 {
     _fileService      = fileService;
     _mimeTypeProvider = mimeTypeProvider;
 }