public async void GetFileAsync(FileContext context)
        {
            string fullPath = @"WebRoot\index.html";

            IInputStream content;
            StorageFile file;

            try
            {
                file = await _installLocation.GetFileAsync(fullPath) as StorageFile;
                content = await file.OpenSequentialReadAsync();
                //IInputStream stream = content.GetInputStreamAt(0);
                context.SetFile(file, content, DateTime.MaxValue);
                content.Dispose();

            }
            catch (FileNotFoundException)
            {
                context.SetFile(null, null, DateTime.MaxValue);
                return;
            }
        }
Пример #2
0
        /// <summary>
        /// Handle the request.
        /// </summary>
        /// <param name="context">HTTP context</param>
        /// <returns><see cref="ModuleResult.Stop"/> will stop all processing except <see cref="IHttpModule.EndRequest"/>.</returns>
        /// <remarks>Invoked in turn for all modules unless you return <see cref="ModuleResult.Stop"/>.</remarks>
        public ModuleResult HandleRequest(IHttpContext context)
        {
            // only handle GET and HEAD
            if (context.Response.RequestMessage.Method != HttpMethod.Get
                && context.Response.RequestMessage.Method != HttpMethod.Head)
                return ModuleResult.Continue;

            var date = context.Response.RequestMessage.Headers.IfModifiedSince.GetValueOrDefault(DateTime.MinValue).UtcDateTime;

            FileContext fileContext = new FileContext(context.RouteUri, date);

            _fileService.GetFileAsync(fileContext);
            //var test = await _fileService.GetFile(fileContext);


            if (!fileContext.IsFound)
                return ModuleResult.Continue;

            if (!fileContext.IsModified)
            {
                context.Response.StatusCode = HttpStatusCode.NotModified;
                context.Response.ReasonPhrase = "Was last modified " + fileContext.LastModified.ToString("R");
                return ModuleResult.Stop;
            }

            var mimeType = MimeTypeProvider.Instance.Get(fileContext.File.Name);
            if (mimeType == null)
            {
                context.Response.StatusCode = HttpStatusCode.UnsupportedMediaType;
                context.Response.ReasonPhrase = string.Format("File type '{0}' is not supported.",
                                                                   Path.GetExtension(fileContext.File.Name));
                return ModuleResult.Stop;
            }
            context.Response.Content = new HttpStreamContent(fileContext.FileContent);
            context.Response.Content.Headers.Add("Last-Modified", fileContext.LastModified.ToString("R"));
            //context.Response.Headers.Add("Accept-Ranges", "bytes");
            context.Response.Content.Headers.Add("Content-Disposition", "inline;filename=\"" + Path.GetFileName(fileContext.File.Name) + "\"");
            context.Response.Content.Headers.ContentType = HttpMediaTypeHeaderValue.Parse(mimeType);
            //context.Response.Content.Headers.ContentLength = (ulong)fileContext.FileContent.Length;


            // do not include a body when the client only want's to get content information.
            if (context.Response.RequestMessage.Method == HttpMethod.Head && context.Response.Content != null)
            {
                context.Response.Content.Dispose();
                context.Response.Content = null;
            }
            return ModuleResult.Stop;
        }