Esempio n. 1
0
        public bool ProcessMarkdownPage(IRequest httpReq, MarkdownPage markdownPage, object dto, IResponse httpRes)
        {
            httpRes.AddHeaderLastModified(markdownPage.GetLastModified());

            var    renderInTemplate = true;
            var    renderHtml       = true;
            string format;

            if (httpReq != null && (format = httpReq.QueryString["format"]) != null)
            {
                renderHtml = !(format.StartsWithIgnoreCase("markdown") ||
                               format.StartsWithIgnoreCase("text") ||
                               format.StartsWithIgnoreCase("plain"));
                renderInTemplate = !httpReq.GetFormatModifier().StartsWithIgnoreCase("bare");
            }

            if (!renderHtml)
            {
                httpRes.ContentType = MimeTypes.PlainText;
            }

            var template    = httpReq.GetTemplate();
            var markup      = RenderDynamicPage(markdownPage, markdownPage.Name, dto, renderHtml, renderInTemplate, template);
            var markupBytes = markup.ToUtf8Bytes();

            httpRes.OutputStream.Write(markupBytes, 0, markupBytes.Length);

            return(true);
        }
        public bool ProcessMarkdownPage(IRequest httpReq, MarkdownPage markdownPage, object dto, IResponse httpRes)
        {
            httpRes.AddHeaderLastModified(markdownPage.GetLastModified());

            var renderInTemplate = true;
            var renderHtml = true;
            string format;
            if (httpReq != null && (format = httpReq.QueryString["format"]) != null)
            {
                renderHtml = !(format.StartsWithIgnoreCase("markdown")
                    || format.StartsWithIgnoreCase("text")
                    || format.StartsWithIgnoreCase("plain"));
                renderInTemplate = !httpReq.GetFormatModifier().StartsWithIgnoreCase("bare");
            }

            if (!renderHtml)
            {
                httpRes.ContentType = MimeTypes.PlainText;
            }

            var template = httpReq.GetTemplate();
            var markup = RenderDynamicPage(markdownPage, markdownPage.Name, dto, renderHtml, renderInTemplate, template);
            var markupBytes = markup.ToUtf8Bytes();
            httpRes.OutputStream.Write(markupBytes, 0, markupBytes.Length);

            return true;
        }
Esempio n. 3
0
        private bool CacheAndWriteResponse(CacheInfo cacheInfo, IRequest req, IResponse res, object response)
        {
            var httpResult = response as IHttpResult;
            var dto = httpResult != null ? httpResult.Response : response;
            if (dto == null || dto is IPartialWriter || dto is IStreamWriter)
                return false;

            var expiresIn = cacheInfo.ExpiresIn.GetValueOrDefault(DefaultExpiresIn);
            var cache = cacheInfo.LocalCache ? HostContext.LocalCache : HostContext.Cache;

            var responseBytes = dto as byte[];
            if (responseBytes == null)
            {
                var rawStr = dto as string;
                if (rawStr != null)
                    responseBytes = rawStr.ToUtf8Bytes();
                else
                {
                    var stream = dto as Stream;
                    if (stream != null)
                        responseBytes = stream.ReadFully();
                }
            }

            var encoding = req.GetCompressionType();
            var cacheKeyEncoded = encoding != null ? cacheInfo.CacheKey + "." + encoding : null;
            if (responseBytes != null || req.ResponseContentType.IsBinary())
            {
                if (responseBytes == null)
                    responseBytes = HostContext.ContentTypes.SerializeToBytes(req, dto);

                cache.Set(cacheInfo.CacheKey, responseBytes, expiresIn);

                if (encoding != null)
                {
                    res.AddHeader(HttpHeaders.ContentEncoding, encoding);
                    responseBytes = responseBytes.CompressBytes(encoding);
                    cache.Set(cacheKeyEncoded, responseBytes, expiresIn);
                }
            }
            else
            {
                var serializedDto = req.SerializeToString(dto);
                if (req.ResponseContentType.MatchesContentType(MimeTypes.Json))
                {
                    var jsonp = req.GetJsonpCallback();
                    if (jsonp != null)
                        serializedDto = jsonp + "(" + serializedDto + ")";
                }

                responseBytes = serializedDto.ToUtf8Bytes();
                cache.Set(cacheInfo.CacheKey, responseBytes, expiresIn);

                if (encoding != null)
                {
                    responseBytes = responseBytes.CompressBytes(encoding);
                    cache.Set(cacheKeyEncoded, responseBytes, expiresIn);
                    res.AddHeader(HttpHeaders.ContentEncoding, encoding);
                }
            }

            var doHttpCaching = cacheInfo.MaxAge != null || cacheInfo.CacheControl != CacheControl.None;
            if (doHttpCaching)
            {
                var cacheControl = BuildCacheControlHeader(cacheInfo);
                if (cacheControl != null)
                {
                    var lastModified = cacheInfo.LastModified.GetValueOrDefault(DateTime.UtcNow);
                    cache.Set("date:" + cacheInfo.CacheKey, lastModified, expiresIn);
                    res.AddHeaderLastModified(lastModified);
                    res.AddHeader(HttpHeaders.CacheControl, cacheControl);

                    if (encoding != null)
                        res.AddHeader(HttpHeaders.Vary, "Accept-Encoding");
                    if (cacheInfo.VaryByUser)
                        res.AddHeader(HttpHeaders.Vary, "Cookie");
                }
            }

            if (httpResult != null)
            {
                foreach (var header in httpResult.Headers)
                {
                    res.AddHeader(header.Key, header.Value);
                }
            }

            res.WriteBytesToResponse(responseBytes, req.ResponseContentType);
            return true;
        }
Esempio n. 4
0
        private async Task <bool> CacheAndWriteResponse(CacheInfo cacheInfo, IRequest req, IResponse res, object response)
        {
            var httpResult = response as IHttpResult;
            var dto        = httpResult != null ? httpResult.Response : response;

            if (dto == null || dto is IPartialWriter || dto is IPartialWriterAsync || dto is IStreamWriter || dto is IStreamWriterAsync)
            {
                return(false);
            }

            var expiresIn = cacheInfo.ExpiresIn.GetValueOrDefault(DefaultExpiresIn);
            var cache     = cacheInfo.LocalCache ? HostContext.AppHost.GetMemoryCacheClient(req) : HostContext.AppHost.GetCacheClient(req);

            var responseBytes = dto as byte[];

            if (responseBytes == null)
            {
                if (dto is string rawStr)
                {
                    responseBytes = rawStr.ToUtf8Bytes();
                }
                else
                {
                    if (dto is Stream stream)
                    {
                        responseBytes = stream.ReadFully();
                    }
                }
            }

            var encoding = !cacheInfo.NoCompression
                ? req.GetCompressionType()
                : null;

            if (response is HttpResult customResult)
            {
                if (customResult.View != null)
                {
                    req.Items[Keywords.View] = customResult.View;
                }
                if (customResult.Template != null)
                {
                    req.Items[Keywords.Template] = customResult.Template;
                }
            }

            using (httpResult?.ResultScope?.Invoke())
                using (HostContext.Config.AllowJsConfig ? JsConfig.CreateScope(req.QueryString[Keywords.JsConfig]) : null)
                {
                    var cacheKeyEncoded = encoding != null ? cacheInfo.CacheKey + "." + encoding : null;
                    if (responseBytes != null || req.ResponseContentType.IsBinary())
                    {
                        if (responseBytes == null)
                        {
                            responseBytes = HostContext.ContentTypes.SerializeToBytes(req, dto);
                        }

                        cache.Set(cacheInfo.CacheKey, responseBytes, expiresIn);

                        if (encoding != null)
                        {
                            res.AddHeader(HttpHeaders.ContentEncoding, encoding);
                            responseBytes = responseBytes.CompressBytes(encoding);
                            cache.Set(cacheKeyEncoded, responseBytes, expiresIn);
                        }
                    }
                    else
                    {
                        var serializedDto = req.SerializeToString(dto);
                        if (req.ResponseContentType.MatchesContentType(MimeTypes.Json))
                        {
                            var jsonp = req.GetJsonpCallback();
                            if (jsonp != null)
                            {
                                serializedDto = jsonp + "(" + serializedDto + ")";
                            }
                        }

                        responseBytes = serializedDto.ToUtf8Bytes();
                        cache.Set(cacheInfo.CacheKey, responseBytes, expiresIn);

                        if (encoding != null)
                        {
                            res.AddHeader(HttpHeaders.ContentEncoding, encoding);
                            responseBytes = responseBytes.CompressBytes(encoding);
                            cache.Set(cacheKeyEncoded, responseBytes, expiresIn);
                        }
                    }
                }

            var doHttpCaching = cacheInfo.MaxAge != null || cacheInfo.CacheControl != CacheControl.None;

            if (doHttpCaching)
            {
                var cacheControl = BuildCacheControlHeader(cacheInfo);
                if (cacheControl != null)
                {
                    var lastModified = cacheInfo.LastModified.GetValueOrDefault(DateTime.UtcNow);
                    cache.Set("date:" + cacheInfo.CacheKey, lastModified, expiresIn);
                    res.AddHeaderLastModified(lastModified);
                    res.AddHeader(HttpHeaders.CacheControl, cacheControl);

                    if (encoding != null)
                    {
                        res.AddHeader(HttpHeaders.Vary, "Accept-Encoding");
                    }
                    if (cacheInfo.VaryByUser)
                    {
                        res.AddHeader(HttpHeaders.Vary, "Cookie");
                    }
                }
            }

            if (httpResult != null)
            {
                foreach (var header in httpResult.Headers)
                {
                    res.AddHeader(header.Key, header.Value);
                }
            }

            await res.WriteBytesToResponse(responseBytes, req.ResponseContentType);

            return(true);
        }
Esempio n. 5
0
        private bool CacheAndWriteResponse(CacheInfo cacheInfo, IRequest req, IResponse res, object response)
        {
            var httpResult = response as IHttpResult;
            var dto        = httpResult != null ? httpResult.Response : response;

            if (dto == null || dto is IPartialWriter || dto is IStreamWriter)
            {
                return(false);
            }

            var expiresIn = cacheInfo.ExpiresIn.GetValueOrDefault(DefaultExpiresIn);
            var cache     = cacheInfo.LocalCache ? HostContext.LocalCache : HostContext.Cache;

            var responseBytes = dto as byte[];

            if (responseBytes == null)
            {
                var rawStr = dto as string;
                if (rawStr != null)
                {
                    responseBytes = rawStr.ToUtf8Bytes();
                }
                else
                {
                    var stream = dto as Stream;
                    if (stream != null)
                    {
                        responseBytes = stream.ReadFully();
                    }
                }
            }

            var encoding        = req.GetCompressionType();
            var cacheKeyEncoded = encoding != null ? cacheInfo.CacheKey + "." + encoding : null;

            if (responseBytes != null || req.ResponseContentType.IsBinary())
            {
                if (responseBytes == null)
                {
                    responseBytes = HostContext.ContentTypes.SerializeToBytes(req, dto);
                }

                cache.Set(cacheInfo.CacheKey, responseBytes, expiresIn);

                if (encoding != null)
                {
                    res.AddHeader(HttpHeaders.ContentEncoding, encoding);
                    responseBytes = responseBytes.CompressBytes(encoding);
                    cache.Set(cacheKeyEncoded, responseBytes, expiresIn);
                }
            }
            else
            {
                var serializedDto = req.SerializeToString(dto);
                if (req.ResponseContentType.MatchesContentType(MimeTypes.Json))
                {
                    var jsonp = req.GetJsonpCallback();
                    if (jsonp != null)
                    {
                        serializedDto = jsonp + "(" + serializedDto + ")";
                    }
                }

                responseBytes = serializedDto.ToUtf8Bytes();
                cache.Set(cacheInfo.CacheKey, responseBytes, expiresIn);

                if (encoding != null)
                {
                    responseBytes = responseBytes.CompressBytes(encoding);
                    cache.Set(cacheKeyEncoded, responseBytes, expiresIn);
                    res.AddHeader(HttpHeaders.ContentEncoding, encoding);
                }
            }

            var doHttpCaching = cacheInfo.MaxAge != null || cacheInfo.CacheControl != CacheControl.None;

            if (doHttpCaching)
            {
                var cacheControl = BuildCacheControlHeader(cacheInfo);
                if (cacheControl != null)
                {
                    var lastModified = cacheInfo.LastModified.GetValueOrDefault(DateTime.UtcNow);
                    cache.Set("date:" + cacheInfo.CacheKey, lastModified, expiresIn);
                    res.AddHeaderLastModified(lastModified);
                    res.AddHeader(HttpHeaders.CacheControl, cacheControl);

                    if (encoding != null)
                    {
                        res.AddHeader(HttpHeaders.Vary, "Accept-Encoding");
                    }
                    if (cacheInfo.VaryByUser)
                    {
                        res.AddHeader(HttpHeaders.Vary, "Cookie");
                    }
                }
            }

            if (httpResult != null)
            {
                foreach (var header in httpResult.Headers)
                {
                    res.AddHeader(header.Key, header.Value);
                }
            }

            res.WriteBytesToResponse(responseBytes, req.ResponseContentType);
            return(true);
        }