コード例 #1
0
        private static void Finish200(IHostServer server, WebContext context, IWebFileRecord staticdescriptor)
        {
            var filetime = context.SetLastModified(staticdescriptor.Version);

            if (filetime <= context.GetIfModifiedSince())
            {
                context.Finish("", status: 304);
            }
            else
            {
                context.SetHeader("Qorpent-Disposition", staticdescriptor.FullName);
                if (server.Config.Cached.Contains(Path.GetFileName(staticdescriptor.FullName)))
                {
                    context.SetHeader("Cache-Control", "public, max-age=86400");
                }
                else if (server.Config.ForceNoCache)
                {
                    context.SetHeader("Cache-Control", "no-cache, must-revalidate");
                }
                else
                {
                    context.SetHeader("Cache-Control", "public");
                }

                RangeDescriptor range = null;
                if (0 < staticdescriptor.Length && (staticdescriptor.MimeType.StartsWith("image/") || staticdescriptor.MimeType.StartsWith("video/")))
                {
                    context.Response.ConentLength = staticdescriptor.Length;
                    if (staticdescriptor.Length > 4096)
                    {
                        context.Response.SetHeader("Accept-Ranges", "bytes");
                    }
                    var rangeHeader = context.Request.GetHeader("Range");
                    if (!string.IsNullOrWhiteSpace(rangeHeader))
                    {
                        var rangeparts = rangeHeader.Substring(6).SmartSplit(false, true, '-');
                        range = new RangeDescriptor {
                            Total = staticdescriptor.Length
                        };
                        range.Finish = range.Total - 1;
                        range.Start  = rangeparts[0].ToInt();
                        if (rangeparts.Count > 1)
                        {
                            range.Finish = rangeparts[1].ToInt();
                        }
                    }
                }

                try {
                    if (staticdescriptor.IsFixedContent)
                    {
                        if (null != staticdescriptor.FixedData)
                        {
                            context.Finish(staticdescriptor.FixedData, staticdescriptor.MimeType, range: range);
                        }
                        else
                        {
                            context.Finish(staticdescriptor.FixedContent, staticdescriptor.MimeType, range: range);
                        }
                    }
                    else
                    {
                        using (var s = staticdescriptor.Open()) {
                            context.Finish(s, staticdescriptor.MimeType, range: range);
                        }
                    }
                }
                catch (Exception e) {
                    Console.WriteLine(e.GetType().Name);
                    context.Finish(e.ToString(), status: 500);
                }
                finally {
                    context.Response.Close();
                }
            }
        }